尽可能防止活动被破坏 [英] Prevent activity from being destroyed as long as possible

查看:193
本文介绍了尽可能防止活动被破坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这个应用程序的常见用例是,您启动它并将手机放在远离它的位置并且每隔一段时间,您都会回到手机并插入一些数据...这是一个日志应用程序,您正在做一些事情并将您的结果插入到应用程序中......



我有这个问题,我的活动会不时地被破坏,并被一个空的包重新创建......(大多数时候情况并非如此,但偶尔会发生这种情况......) 。我的应用程序有时会启动一个服务,即使这个服务在这种情况下也被杀死了......



这意味着系统已经杀死了我的应用程序,是吗?我怎样才能避免这种情况?



我需要保留用户数据和当前顶级片段......并将它们保存到包中,一切正常只要他们的状态和数据得到保存......



顺便说一句,我的活动总是最高活动,只是经常关闭屏幕......我只是希望尽可能长时间保持我的活动状态,直到用户使用后退按钮...或者保存状态可靠

重要提示
$ b

onSaveInstance并不总是能够工作(它不是生命周期的一部分,因此不能保证被调用)......它只能工作在大部分时间...我需要一种方式来工作总是...如果android杀死我的应用程序...

h2>不要将您的应用程序保存在内存中

您不想阻止Android杀死您的应用程序。你想要的是正确地恢复你的应用程序的状态。然后,用户将永远不会注意到应用程序已被销毁,用户仍然可以获得不使用时被销毁的应用程序的好处。



如果你真的想要使用唤醒锁。这将耗尽你的用户电池,所以我认为在实施这个之前要三倍...信息在如何防止Android设备以编程方式进入睡眠状态?



onSaveInstanceState解释 h2>

为此,请检查包中需要的信息,并使用 onSaveInstanceState(bundle:Bundle)方法因此您可以在 onCreate(sameBundle:Bundle)中重复使用它。



更多信息可从Google文档保存你的活动状态恢复您的活动状态



关于An droid Activity lifecycle



正如下面评论中的@ prom85所述,它不能保证 onSaveInstanceState 方法会被调用因为它不是生命周期的一部分。解决方法是使用 onPause 生命周期钩子来确保存储数据。

=https://stackoverflow.com/questions/12793069/android-onsaveinstancestate-not-being-called-from-activity> Android:onSaveInstanceState不会从活动调用


I have an app, a single activity app with fragments in it.

The usual use case for this app is, that you start it and put the phone away and every now and then, you get back to the phone and insert some data... It's a logging app, you are doing something and insert your results into the app...

I have the problem, that every now and then, my activity get's destroyed and is recreated with an empty bundle... (Most of the time this is not the case, but every now and then this happens...). My app sometimes starts a service, even this service is killed in this case...

This means, that the system has killed my app, does it? How can I avoid this?

I need to keep the user data and the current top fragments... And they are saved to the bundle and everything works as long as their states and the data get saved...

Btw., my activity is always the TOP ACTIVITY, only that the screen turns off often... I just want to keep my activity alive as long as possible until the user leaves it with the back button... Or to save the state reliably

IMPORTANT NOTE

onSaveInstance does not always work (it's not part of the lifecycle and therefore not guaranteed to be called)... it only works most of the time... I need a way to that works always... If android kills my app...

解决方案

don't keep your app in memory

You don't want to block Android from killing your app. What you want is to restore your app's state properly. Then the user will never notice the app has been destroyed and the user still gets the benefit of an app that was destroyed when not in use.

If you really want this use a wakelock. This will drain your users battery so I think twice before implementing this... Info at How do I prevent an Android device from going to sleep programmatically?

onSaveInstanceState explained

To do so check what information is needed in the bundle and persist that information with the onSaveInstanceState(bundle:Bundle) method so you can reuse it in onCreate(sameBundle:Bundle).

More information available from Google documentation at Save your Activity state and Restore your Activity State.

About Android Activity lifecycle

As stated by @prom85 in the comments below it's not guaranteed that the onSaveInstanceState method will be called because it's not part of the lifecycle. Workaround for this is using the onPause lifecycle hook to ensure your data is stored.

More information at Android: onSaveInstanceState not being called from activity

这篇关于尽可能防止活动被破坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆