简体   繁体   English

Android有时会强制杀死应用程序

[英]Android sometimes force kills application

I start activity A, then start activity B.我开始活动 A,然后开始活动 B。
I press home button, then waiting long time.我按下主页按钮,然后等待很长时间。
When I resume application, it force stopped.当我恢复应用程序时,它强制停止。

02-03 18:42:54.413 828-844/system_process I/ActivityManager: Force stopping ru.tabor.search appid=10089 user=0: from pid 20405
02-03 18:42:54.414 828-844/system_process I/ActivityManager: Killing 30212:ru.tabor.search/u0a89 (adj 7): stop ru.tabor.search
02-03 18:42:54.445 828-5948/system_process I/WindowState: WIN DEATH: Window{18b92c9b u0 ru.tabor.search/ru.tabor.search.modules.authorization.AuthorizationActivity}
02-03 18:42:54.447 828-845/system_process I/WindowState: WIN DEATH: Window{1cd0cfe4 u0 ru.tabor.search/ru.tabor.search.modules.registration.RegistrationActivity}
02-03 18:42:54.519 828-844/system_process I/ActivityManager:   Force finishing activity 3 ActivityRecord{25a8977f u0 ru.tabor.search/.modules.authorization.AuthorizationActivity t2593}
02-03 18:42:54.520 828-844/system_process I/ActivityManager:   Force finishing activity 3 ActivityRecord{d516838 u0 ru.tabor.search/.modules.registration.RegistrationActivity t2593}
02-03 18:42:54.523 828-20666/system_process W/ActivityManager: Spurious death for ProcessRecord{21ff313b 0:ru.tabor.search/u0a89}, curProc for 30212: null
02-03 18:42:59.890 828-1247/system_process I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10100000 cmp=ru.tabor.search/.modules.authorization.AuthorizationActivity} from uid 10089 on display 0
02-03 18:42:59.903 828-1247/system_process V/WindowManager: addAppToken: AppWindowToken{1c4987a0 token=Token{279a08a3 ActivityRecord{9f5afd2 u0 ru.tabor.search/.modules.authorization.AuthorizationActivity t2593}}} to stack=1 task=2593 at 0
02-03 18:42:59.919 828-891/system_process V/WindowManager: Adding window Window{1735e91b u0 Starting ru.tabor.search} at 4 of 8 (after Window{2ab6bf53 u0 com.cleanmaster.mguard/com.keniu.security.main.MainActivity})
02-03 18:43:19.288 828-1673/system_process I/ActivityManager: Start proc 21366:ru.tabor.search/u0a89 for activity ru.tabor.search/.modules.authorization.AuthorizationActivity

How to fix it?如何解决?

As mush as i know , you cant do anything about that !!!据我所知,你对此无能为力! that is the behavior of android garbage collection system !这就是android垃圾收集系统的行为!

as i remember, the android do garbage collection by itself, if you do not use an activity for a long time in background, it will be garbage collected by the system to make ram free for other apps and processes.我记得,android 自己做垃圾收集,如果你在后台很长时间不使用活动,它会被系统收集垃圾,以便为其他应用程序和进程腾出内存。

if you have any information in that activity which you want to keep, save it here;如果您想保留该活动中的任何信息,请将其保存在这里;

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
}

when you back to that activity,当你回到那个活动时,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState!=null){
    // GET #savedInstanceState AND USE THE OBJECT YOU STORED
    }
}

thats all you can do.这就是你所能做的。

Do you pass something as an Intent extra to the activity?您是否将某些内容作为 Intent extra 传递给活动? If not, try removing clean master from your device.如果没有,请尝试从您的设备中删除干净的主。

is your app leaking?你的应用程序泄漏了吗? if the phone is running low in memory, it will kill the memory hog app.如果手机内存不足,它将杀死内存猪应用程序。 if there is a process running on those activity, transfer it to a service and call startForeground() inside oncreate.如果有一个进程在这些活动上运行,则将其转移到服务并在 oncreate 中调用 startForeground()。

Recreating an Activity says: 重新创建一个活动说:

The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the foreground activity requires more resources so the system must shut down background processes to recover memory.如果您的活动当前已停止并且长时间未使用,或者前台活动需要更多资源,系统也可能会破坏您的活动,因此系统必须关闭后台进程以恢复内存。

So you can't stop system from killing your application.所以你不能阻止系统杀死你的应用程序。

To overcome it you should override onRestoreInstanceState method.要克服它,您应该重写onRestoreInstanceState方法。 In this case you can save state of activity when system kills it and then restore when you navigating back to this activity.在这种情况下,您可以在系统终止活动时保存活动状态,然后在导航回此活动时恢复。

Another option is implementing own IntentService .另一种选择是实现自己的IntentService

Quote from Services API guide:引用服务API 指南:

The Android system will force-stop a service only when memory is low and it must recover system resources for the activity that has user focus. Android 系统仅在内存不足时才会强制停止服务,并且必须为具有用户焦点的活动恢复系统资源。

Apparently Android memory is limited so Virtual Machine can remove any piece of code that might be un-necessary.显然 Android 内存是有限的,因此虚拟机可以删除任何可能不必要的代码。

Have a look into Activity life-cycle method, specially into onResume and make sure that you understand that perfectly.查看Activity 生命周期方法,特别是onResume并确保您完全理解这一点。 So many time application crashes just for improper use Activity life-cycle's methods.很多时候应用程序崩溃只是因为不正确使用 Activity 生命周期的方法。

Another important part is design consideration for Activity is, no matter what happened with persistence data your Activity should display its UI with some default value.另一个重要的部分是 Activity 的设计考虑因素是,无论持久性数据发生了什么,您的 Activity 都应该使用一些默认值显示其 UI。 So assumption is like this, if I have data I will display if I don't, I really do not care .所以假设是这样的,如果我有数据我会显示,如果我没有,我真的不在乎 Your UI should never crash with or without data .无论有无数据,您的 UI 都不应崩溃 You can use Resources , say for example String.xml , dimens.xml for storing some default value or even in layouts.您可以使用Resources ,例如String.xmldimens.xml来存储一些默认值,甚至在布局中。

If you still want go with singleton class, which is perfectly fine but make sure you do the following checking every time you try to access your singleton.如果您仍然想使用单例类,这很好,但请确保每次尝试访问单例时都进行以下检查。

if (instance==null)
    instance=CurrentActivity.getInstance()

getInstance() method not only return you current instance it will also make sure that getInstance()方法不仅返回您当前的实例,它还将确保

  • It initializes all object and variable它初始化所有对象和变量
  • Other singleton methods as instance method其他单例方法作为实例方法

Do not statically access data from one Activity to another.不要静态地从一个活动访问数据到另一个活动。 It is not good for android specially for the type problem you are facing now and also it is not very good OOP programming practice.对于您现在面临的类型问题,这对 android 来说并不好,而且它也不是很好的 OOP 编程实践。

I recommended SharedPreference .我推荐SharedPreference Too much better way to persist data, if that meet your requirement go for it.持久化数据的更好方法,如果满足您的要求,那就去吧。

If you want to pass data from different Android component like Activity, Service or BroadcastReciever you can put it inside a bundle and and send as intent.如果您想从不同的 Android 组件(如 Activity、Service 或 BroadcastReciever)传递数据,您可以将其放入包中并作为意图发送。 And, as always their are SQLLite data storage, file IO etc etc.而且,它们一如既往地是 SQLLite 数据存储、文件 IO 等。

Hope this will help you.希望这会帮助你。

很可能您的应用程序由于内存过载而被 android OS 杀死,或者可能存在某种情况下您的主线程中正在运行某种进程,您需要对其进行检查。同时覆盖 OnResume 函数并检查您得到的结果。

I know this question is very old, but for those people still looking for answer, Try turning Battery Optimisation and Doze Mode off for your particular app from Android Settings.我知道这个问题已经很老了,但是对于那些仍在寻找答案的人,请尝试从 Android 设置中为您的特定应用关闭电池优化和打盹模式。 This should work.这应该有效。

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

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