简体   繁体   English

强制我的活动调用onSavedInstance状态

[英]Force My Activity To Call The onSavedInstance State

I have an android application in which I use two themes. 我有一个使用两个主题的android应用程序。 On theme change I need to recreate all the activities and Fragments inside that to be recreated. 关于主题更改,我需要重新创建要重新创建的所有活动和片段。

The same way our application get recreated using saved instnce state. 使用保存的实例状态重新创建我们的应用程序的方式相同。

Is there any way I can force my Activity to call the saved instance state ? 有什么办法可以强制我的Activity调用保存的实例状态? IF it is possible how I can do that ? 如果有可能我该怎么做? If not why ? 如果不是为什么? Is there any alternative to Achieve this ? 是否有其他方法可以实现这一目标?

My current code looks like below. 我当前的代码如下所示。

public static final void restartActivity(final FragmentActivity activity){
    activity.finish();
    activity.startActivity(new Intent(activity, activity.getClass()));
}

The issue with this is the fragment back stack is not getting recreated. 问题是没有重新创建片段回栈。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Your state is not saved because you're creating a new instance of Activity . 您的状态未保存,因为您正在创建Activity的新实例。 As a solution, you can pass your state to this new instance as Intent extras and restore state in onCreate from those extras. 作为解决方案,您可以将状态作为Intent Extras传递给此新实例,并从这些Extras中还原onCreate状态。

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

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