【发布时间】:2013-01-05 21:55:33
【问题描述】:
我有两个活动,A 和 B。当活动 A 首次启动时,它会访问传递给它的 Intent(因为 Bundle 是 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;
}
...
}
问题就出在这里,当第二次在活动A的onCreate()中,Bundle参数是null并且getExtras()返回null。由于调用了onSaveInstanceState(),我预计Bundle 参数不是null。
我在其他网站上阅读过有关此问题的信息,尝试了建议,但没有任何效果。
【问题讨论】:
-
finish() 是使用 Activity B 的方式。是的,如果您保存在 onSaveInstanceState() 中,onCreate 应该与它捆绑在一起。三重检查您的代码,因为其中一定存在错误。您也可以考虑发布更完整的代码。
-
jsmith -- 我试图保持代码简短但相关。我还能添加什么有帮助的内容?
-
3 年后,我们遇到了同样的问题
标签: android android-activity android-actionbar up-button