Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android--多個Ativity值傳遞FLAG_ACTIVITY_FORWARD_RESULT

android--多個Ativity值傳遞FLAG_ACTIVITY_FORWARD_RESULT

編輯:關於Android編程

適用情況:多個Activity的值傳遞。ActivityA到達ActivityB再到達ActivityC,但ActivityB為過渡頁可以finish了,此時ActivityC將值透傳至ActivityA。

其實只要利用Intent的FLAG_ACTIVITY_FORWARD_RESULT即可。

第一個頁面:A跳到B

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));
    	}
    }
}
過渡頁:中間有再多過渡頁也是一樣。B跳到C

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();
	}

}
C傳值然後在A的OnActivityResult中獲取值

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();
	}

}




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved