[英]Passing extras from Activity to Intent throws NullPointerException
[英]Passing information from an Activity to an Intent and back using Extras
我有一個活動,它會啟動一個包含一些額外信息的Intent。
intent.putExtra("DATABUNDLE", createEmptyLocationBundle());
startActivity(intent);
createEmptyLocationBundle()
僅返回其中包含一些數據的Bundle對象。
從啟動的Intent中,我可以毫無問題地獲取信息。
我可以從Intent內修改捆綁軟件的信息,也沒有問題。
但是,如何將修改后的捆綁軟件發回原始活動呢?
謝謝。
正如這里所說的http://developer.android.com/training/basics/intents/result.html
在主要活動覆蓋方法中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//Do something here
}
}
然后在主要活動中也使用此方法開始意圖:
startActivityForResult(intent.putExtra("DATABUNDLE", createEmptyLocationBundle()), 0);
在啟動的活動中,使用以下方法傳回數據:
Intent intent = new Intent();
intent.putExtra("name", etName.getText().toString());
setResult(RESULT_OK, intent);
this.finish();
你不能這樣 您必須致電以下才能開始活動
startActivityForResult(intent, requestCode);
並為結果覆蓋onActivityResult
方法。
在目標活動中
Intent data = new Intent();
data.putExtra("key",data);
setResult(RESULT_OK,data);
finish();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.