简体   繁体   中英

Android sometimes force kills application

I start activity A, then start activity 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 !

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.

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? 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.

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. 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 .

Quote from Services API guide:

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.

Apparently Android memory is limited so Virtual Machine can remove any piece of code that might be un-necessary.

Have a look into Activity life-cycle method, specially into onResume and make sure that you understand that perfectly. So many time application crashes just for improper use Activity life-cycle's methods.

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. 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 . You can use Resources , say for example String.xml , dimens.xml for storing some default value or even in layouts.

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

  • 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.

I recommended 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. And, as always their are SQLLite data storage, file IO etc etc.

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. This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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