[英]Parent activity keeps reseting when navigating back from child
When I use an intent to move from parent activity to another, and then use action bar nav to go back to parent, the parent activity has lost all my changes and values (as a user) and is back to default xml display i have for it. 当我使用意图从父级活动移到另一个活动,然后使用导航栏导航返回父级时,父级活动丢失了我的所有更改和值(作为用户),并返回到我具有的默认xml显示它。
I have these in my Child activity OnCreate: 我的Child活动OnCreate中有这些:
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
And labeled my parent activity appropiately: 并适当地标记了我的父母活动:
android:parentActivityName=".MainActivity"
What do I need to make it store or remember those changes when traveling between activities? 在两次活动之间旅行时,我需要什么来存储或记住这些更改?
There are couple of options to try - 有几种选择可以尝试-
1) start the new activity with startActivityForResult()
1)使用
startActivityForResult()
启动新活动
2) in the manifest file, set the android:launchMode="singleTop"
of the parent activity 2)在清单文件中,设置父活动的
android:launchMode="singleTop"
Hope it helps 希望能帮助到你
In the Manifest file, you should set android:launchMode like this: 在清单文件中,您应该像这样设置android:launchMode:
<!-- ACTIVITY -->
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:screenOrientation="portrait"/>
Ps: read Launch Mode on this link: https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en 附言:请阅读此链接上的启动模式: https : //inthecheesefactory.com/blog/understand-android-activity-launchmode/en
You might be creating a new instance of your parent when you click the Navigation button. 单击导航按钮时,您可能正在创建父级的新实例。 A simple approach is to just finish your activity so whatever activity in the backstack will simply be resumed.
一种简单的方法是仅完成您的活动,以便简单地恢复后堆栈中的任何活动。
Something like this: 像这样:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.