简体   繁体   English

如何指示操作系统在应用程序闲置数小时后不要重新创建活动,而要重新启动?

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

相关问题 如果应用程序闲置了数小时并被OS破坏,是否在Activity.onResume之前调用Application.onCreate? - Is Application.onCreate called before Activity.onResume if app has been idle for hours and destroyed by OS? 加载webview后如何在android中启动另一个活动? - How to start an another activity in android after webview has been loaded? 应用程序未使用几个小时后碎片消失 - Fragments disappearing after application has not been used for few hours 在Android OS销毁某个活动之后,对活动的引用会如何处理? - What happens to references to an activity after it has been destroyed by the Android OS? 打开应用程序后如何仅重新创建一次活动? - How do I recreate the activity only once after opening the application? Android:如何在动画视图的活动恢复后启动在ImageView上应用的无限动画? - Android: How to start an infinite animation applied on an ImageView after the Activity with the animated View has been resumed? 在应用程序收到短信后如何调用活动? - How do i call an activity after a sms has been received by application? 更改活动后如何获取当前活动 - How to get the current activity after activity has been changed 如果应用程序已还原,如何从活动中获取结果 - How to get result from an activity if an application has been restored 开始活动后如何确定视图大小 - How to determine the View size right after Activity has been started
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM