http://blog.csdn.net/hongshan50/article/details/6585396
这里有一份很好的文章:
http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html
1.无参数Activity跳转
2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)
Intent it = new Intent(Activity.Main.this, Activity2.class);
对于数据的获取可以采用:
或者:
3.向上一个Activity返回结果(使用setResult,针对
4.回调上一个Activity的结果处理函数(onActivityResult)
注意:
总结:调用startActivityForResult,onActivityResult无响应的问题 http://blog.sina.com.cn/s/blog_5da93c8f0101gzf5.html
我碰到问题跟这里的不一样,就是在接受返回参数的Activity里面的一些方法,使用了getIntent()来得到Intent,导致还没运行返回参素好的Activity,就已经调用了onActivityResult();
最好不要在非onCreate()方法外使用getIntent()来获取Intent;
我的例子源码:
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/factorone" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/symbol" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/symbol" /> <EditText android:id="@+id/factortwo" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/calculate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/calculate" /> </LinearLayout> |
result.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/totalRsult" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/symbol" /> </LinearLayout> |
strings.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Android_TwoActivityActivity!</string> <string name="app_name">Android_TwoActivity</string> <string name="firstActivity">第一个Activity</string> <string name="secondActivity">第二个Activity</string> <string name="symbol">乘以</string> <string name="calculate">计算</string> </resources> |
FirstActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.pandy.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class FirstActivity extends Activity { private EditText factorOne; private EditText factorTwo; private TextView symbol; private Button calculate; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); factorOne = (EditText) findViewById(R.id.factorone); factorTwo = (EditText) findViewById(R.id.factortwo); symbol = (TextView) findViewById(R.id.symbol); calculate = (Button) findViewById(R.id.calculate); calculate.setOnClickListener(new MyListener()); } class MyListener implements OnClickListener { @Override public void onClick(View arg0) { Intent intent = new Intent(); intent.setClass(FirstActivity.this, SecondActivity.class); intent.putExtra("factorOneStr", factorOne.getText().toString()); intent.putExtra("factorTwoStr", factorTwo.getText().toString()); startActivity(intent); } } } |
SecondActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.pandy.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { private TextView resultView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result); resultView = (TextView) findViewById(R.id.totalRsult); Intent intent = getIntent(); String factorOneStr = intent.getStringExtra("factorOneStr"); String factorTwoStr = intent.getStringExtra("factorTwoStr"); int factorOneInt = Integer.parseInt(factorOneStr); int factorTwoInt = Integer.parseInt(factorTwoStr); resultView.setText(factorOneInt * factorTwoInt + ""); } } |
R.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.pandy.test; public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int calculate=0x7f050003; public static final int factorone=0x7f050000; public static final int factortwo=0x7f050002; public static final int symbol=0x7f050001; public static final int totalRsult=0x7f050004; } public static final class layout { public static final int main=0x7f030000; public static final int result=0x7f030001; } public static final class string { public static final int app_name=0x7f040001; public static final int calculate=0x7f040005; public static final int firstActivity=0x7f040002; public static final int hello=0x7f040000; public static final int secondActivity=0x7f040003; public static final int symbol=0x7f040004; } } |
AndroidManifest.xml
———————————————-