适用情况:多个Activity的值传递。ActivityA到达ActivityB再到达ActivityC,但ActivityB为过渡页可以finish了,此时ActivityC将值透传至ActivityA。
其实只要利用Intent的FLAG_ACTIVITY_FORWARD_RESULT即可。
第一个页面:A跳到B
<span style="font-size:18px;">public class ActivityA extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, ActivityB.class); startActivityForResult(intent, 100); } @Override protected void onActivityResult(int arg0, int arg1, Intent arg2) { super.onActivityResult(arg0, arg1, arg2); if(arg2 != null) { Log.e("yanru", "requestCode="+arg0+",resultCode="+arg1+",data="+arg2.getIntExtra("a", 8)); } }}</span>过渡页:中间有再多过渡页也是一样。B跳到C
<span style="font-size:18px;">public class ActivityB extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(this, ActivityC.class); intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); startActivity(intent); finish(); }}</span>C传值然后在A的OnActivityResult中获取值
<span style="font-size:18px;">public class ActivityC extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); intent.putExtra("a", 8); setResult(Activity.RESULT_OK, intent); finish(); }}</span>