简体   繁体   English

从孩子返回时,家长活动不断重置

[英]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.

相关问题 Android后退按钮-导航到下一个活动或从下一个活动返回时如何保留父状态 - Android back button - how to preserve parent state when navigating to and back from next activity 从子活动中导航后如何恢复活动数据? - How to restore activity data after navigating back from child activity? 使用工具栏导航回父活动 - Navigating back to Parent activity with toolbar 从另一个活动中导航时是否可以保留子片段的状态? - Possible to retain state of child fragment when navigating back from another activity? Android:当我按系统后退菜单时,从子活动返回后,父母活动数据丢失 - Android: parent activity data is lost after back from child activity when I press system back menu ViewPager:从子片段导航回父片段将关闭应用程序 - ViewPager : Navigating Back to Parent Fragment From Child Fragment Closes The Application 如何在从子活动返回到父活动时关闭接近传感器 - How to turn off proximity sensor when returning back from child activity to parent activity 停止导航回活动 - Stop from navigating back to an activity 如何从ActivityGroup中的子活动返回到父活动? - How to return back to parent activity from child activity in ActivityGroup? 从子活动android返回到父活动 - Move back to parent activity from child activity android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM