如果将Intent数据放在super.onBackPressed()之后,则该intent数据将不会传递到上一个活动中的onActivityResult。为什么?据我了解,一切都在主线程上,因此,即使首先调用super.onBackPressed(),它也应该传递回意图。
如果在/末尾调用super.onBackPressed(),它将起作用。
SettingActivity.java
@Override
public void onBackPressed()
{
Intent i = new Intent();
i.putExtra("test", -999);
setResult(RESULT_OK, i);
super.onBackPressed();
}
When Super.onBackPressed called initially, doesn't work.
SettingActivity.java:
@Override
public void onBackPressed()
{
super.onBackPressed();
Intent i = new Intent();
i.putExtra("test", -999);
setResult(RESULT_OK, i);
//super.onBackPressed(); if called after, it works.
}
MainActivity.java:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SETTING_REQ_CODE)
Log.d(this.getClass().getSimpleName(), data.getIntExtra("test", 0)));
}
如果之前调用了super.onBackPressed(),则记录的消息将打印出默认值0。如果最后调用了super.onBackPressed(),则将检索意图的数据并显示-999。