[英]How to instruct the OS not to recreate activity after application has been idle for hours, but to start fresh instead?
I have read both solutions here: 我在这里阅读了两种解决方案:
Prevent Activity Stack from being Restored? 阻止活动堆栈还原?
But I hope there is a more elegant way to solve this. 但我希望有一种更优雅的方法来解决这个问题。
Here is where my problem originates. 这就是我的问题所在。 I have an Activity that has a View Pager with Fragments Those fragments are inner classes and for some reason inner Fragments should be static.
我有一个带有片段的View Pager的Activity,这些片段是内部类,由于某种原因,内部Fragments应该是静态的。 The fragments however, use a lot of stuff from the parent Activity, that is not static, and cannot be accessed from a static context.
但是,这些片段会使用来自父活动的大量内容,这些内容不是静态的,并且无法从静态上下文访问。 I tried passing them as arguments, but one of the arguments is the ViewPager itself and it cannot be serialized or accessed from a static context.
我尝试将它们作为参数传递,但是其中一个参数是ViewPager本身,无法对其进行序列化或从静态上下文进行访问。 If the app has been idle for hours, the Activity is recreated instead of started fresh, and the non-static inner class Fragment crashes.
如果应用程序闲置了几个小时,则将重新创建“活动”,而不是重新启动它,并且非静态内部类Fragment崩溃。
My activity does not need to remember its state from 5 hours ago. 我的活动不需要记住5个小时前的状态。 I want it to not be restored whenever the app is brought back to foreground again.
我希望每当应用程序再次回到前台时都不会还原它。
I think I found the solution, it is so simple! 我想我找到了解决方案,它是如此简单! (a bit hacky though)
(虽然有点hacky)
@Override
protected void onSaveInstanceState(Bundle outState) {
}
No call to super, so the OS has no information to use when attempting to restore the activity state, so it builds it anew altoghether. 没有对super的调用,因此OS在尝试还原活动状态时没有可用的信息,因此它重新构建了活动状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.