我有两个活动 A 和 B。当活动 A 首次启动时,它会访问Intent
传递给它(因为Bundle
is null
,因为它应该是第一次通过),并相应地显示信息:
CustInfo m_custInfo;
...
protected void onCreate(Bundle savedInstanceState)
{
...
Bundle bundle = (savedInstanceState == null) ? getIntent().getExtras() : savedInstanceState;
m_custInfo = (CustInfo) m_bundle.getSerializable("CustInfo");
if (m_custInfo != null
...
}
第一次这样做效果很好。这EditText
控制和ListView
已正确填写。
现在,当单击列表中的某个项目时,将启动活动 B 以显示详细信息:
m_custInfo = m_arrCustomers.get(pos);
Intent intent = new Intent(A.this, B.class);
intent.putExtra("CustInfo", m_custInfo); // CustInfo is serializable
// printing this intent, it shows to have extras and no flags
startActivityForResult(intent, 1);
在活动 B 开始之前,框架会调用 A 的重写onSaveInstanceState()
:
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putSerializable("CustInfo", m_custInfo);
}
在活动 B 中,当按下操作栏中的向上按钮时,我想返回到活动 A 并使其处于与之前相同的状态:
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == android.R.id.home)
{
Intent intent = NavUtils.getParentActivityIntent(this);
// printing this intent, it shows to have flags but no extras
NavUtils.navigateUpFromSameTask(this); // tried finish() here but that created an even bigger mess
return true;
}
...
}
问题就在这里,当在onCreate()
第二次进行活动 A 时,Bundle
参数是null
and getExtras()
回报null
。自从onSaveInstanceState()
被叫了,我本以为Bundle
参数为非null
.
我在其他网站上读到过有关此问题的信息,并尝试过这些建议,但没有任何效果。