简体   繁体   English

调用 Activity.Recreate() 后维护我的 Activity 后台堆栈的最佳方法是什么?

[英]What is the best way to maintain the backstack of my Activity after Activity.Recreate() is called?

I have an Activity that handles many Fragments , and, for backstack management, I have a custom stack, where I manage show / hide for Fragments .我有一个Activity处理许多Fragments ,并且对于backstack管理,我有一个自定义堆栈,我在其中管理Fragments显示/隐藏。 My code and navigation work perfectly.我的代码和导航工作完美。

Right now, I am implementing the application theme change by a Button in the Configuration Fragment .现在,我正在通过Configuration FragmentButton实现应用程序主题更改 For this I am using the method Activity.Recreate ();为此,我使用方法Activity.Recreate (); for the change of the theme and, the data of the Configuration Fragment is retained with the same data and the theme of the application changes perfectly , but the BackStack of Fragments disappears, reason why, when pressing the back button, it leaves the application, instead of sending me back to the Fragment or Previous Activity, from where I accessed the Configuration Fragment .由于主题的变化, Configuration Fragment的数据保留了相同的数据,应用程序的主题发生了完美的变化,但是BackStack of Fragments消失了,为什么按下后退按钮时它离开了应用程序,而不是将我送回 Fragment 或 Previous Activity,从那里我访问Configuration Fragment

What is the best way to maintain the backstack of my Activity?维护我的 Activity 后台堆栈的最佳方法是什么? This is possible?这个有可能?

Important: only when Activity.Recreate();重要提示:仅当Activity.Recreate(); is called, because if the Activity is destroyed by any other way, I do not want the BackStack back, I want my Activity clean .被调用,因为如果 Activity 被任何其他方式销毁,我不希望 BackStack 回来,我希望我的 Activity clean

Additional:额外的:

  • The orientation setting of my application is in portrait mode .我的应用程序的方向设置是纵向模式
  • The launchMode of my Activity is singleTask , it must be so for the type of application I am doing.launchMode我的活动是singleTask ,一定是这样的应用我做的类型。

From onCreate documentation and this answer.来自onCreate文档和这个答案。

Add the following logic to your code:将以下逻辑添加到您的代码中:

public void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState == null) { 
        // savedInstanceState will be null only when creating the activity for the first time
        backstack = new BackStack(); //init your backstack
    } else {
      // there is a chance that your backstack will be already exists at this point
      // if not:
      // retrieve the backstack with savedInstanceState.getSerializable("stack")
    }
}

And just clear the stack when changing theme, before calling recreate()在调用recreate()之前,更改主题时只需清除堆栈

// changing theme detected
bacstack.clear();
backstack = null;
recreate();

To save the stack between destruction (onDestroy) and recreation (onCreate) of your activity, Use this method:要保存活动的销毁 (onDestroy) 和重新创建 (onCreate) 之间的堆栈,请使用以下方法:

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    if (backstack != null) // the check isn't necessary, you can just put a null in the bundle
        outState.putSerializable("stack", backstack);
}

the official guide for saving UI state保存 UI 状态的官方指南

The onSaveInstanceState method helps your activity to Survive Configuration change and System-initiated process death. onSaveInstanceState方法可帮助您的活动在配置更改和系统启动的进程死亡中幸存下来。 link 关联

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM