why does android 'revives' crashing apps? - android

if you have an android app that has more than one Activity, and Activity A starts B, so if Activity b crashes the process is killed but is revived by android OS and starts Activity A again instead of just shutting down the app, why ?

You're complaining that Android will attempt to recover your app's state gracefully after a crash? ;)
This is the result of Android's resource management and the Activity lifecycle at work. Keep in mind that a single task can be composed of a number of Activities that may span several processes or apps. As outlined here: http://android-developers.blogspot.com/2010/04/multitasking-android-way.html Android processes don't shut down "cleanly" in the traditional *nix process sense. Your app's components receive lifecycle events but after a certain point the app can be killed without further warning so that the system may reclaim its resources.
For example, a user might be browsing the web, click a youtube link launching the youtube app, then click a share button to launch their favorite social networking app and post the video link. This is all part of the same task; if the user presses back several times they'll come back to the page in the browser that they started from.
Once you start up the social networking app, the system might decide that it's running low on memory and it's going to kill off the browser's process to free up more. (After all, it's not in front and the user won't notice.) When the user presses the back button to return to the browser Activity it gets restarted and reconstructs the last state where the user left it. At worst the user experiences a short delay while things reinitialize.
But this same sequence of events restoring a previous Activity state can happen even within the same app in the same process. In your scenario, Activity B closed as the result of the crash. So the system does exactly what it always does - it returns to the previous Activity: Activity A. But Activity A's process isn't still around (it crashed!) so the system restarts it and it can reconstruct its previous state.

Related

What happens when already destroyed activity by OS is revoked by user from recent apps?

As far as I know, an app that is already destroyed by Android System resides in recent apps. For example, I launched my app and navigated through several activities. Then quit, and opened several other apps and not launched my app again for several hours. My last opened activity will stay in the recent apps even though it has been destroyed by the system. My question is what life-cycle methods will be run when I touch my app from the recent apps list and in which order? Also what will happen to my data in the activity that is still shown on the recent apps? Does it get created from the start? Do I still have an activity stack? and will my base application class be re-created also?
According to LifeCycle of an activity, after your app will be killed by the android OS (to get memory for more prioritized apps), your activity will start from onCreate() method, and go through the cycle, as usual) What about your data:
values in views (like EditText) will be restored if your view have ids.
your variable values I suggest you to save and restore with onSaveInstanceState() and onRestoreInstanceState().
There is two scenario here:-
If your app still in recent apps much time and other apps need memory your app will be killed by Android OS, and if you open it again it will start from splash-screen.
If your app stayed short time in recent-apps without need for memory the last activity you used will be opened, starting it's lifecycle from onStart().
Once you press home button It will call onStop without destroying it unless OS or user do.

Difference between closing app via back button and clearing it from recents list?

My app uses a DefaultHttpClient to make network requests. On occasion (usually after resuming the app after a long period of time) the app will stop loading data and I need to clear it from the recent apps list before it loads content again.
My question is, what's the difference between:
- Closing an app by tapping the back button
- Clearing the app from the recent app list
When my app stops loading content, exiting via the back button and reopening does not fix the problem. Only killing the app by clearing it from the recent app list works.
Is there a way to "kill" the app when a user exits with the back button?
Thanks
Closing an app by tapping the back button
Pressing the BACK button, by default, destroys whatever the foreground activity is, returning control to the previous activity (or the home screen if there is no previous activity). It does not "close" an app.
Clearing the app from the recent app list
Usually, this will terminate the app's process. Contrast that with pressing BACK to destroy all of your activities, where the process remains running (at least for a while).
Is there a way to "kill" the app when a user exits with the back button?
Not really. You are better served fixing the bugs in your app.
usually after resuming the app after a long period of time
If "a long period of time" is less than 30 minutes or so, your process may have been terminated while your app was in the background, but Android will try to return the user to wherever the user had been in your app. This involves forking a fresh process for you and recreating your last activity. Sometimes, developers make assumptions that their process always starts with the launcher activity, with bugs being uncovered when the process starts with some other activity.
Also note that the HttpClient implementation in the Android SDK was deprecated in API Level 22 and removed in API Level 23. Either use an independent packaging of HttpClient or use some other HTTP client API (HttpURLConnection, OkHttp, etc.).
when you press back button then onBackpress() calls and app exit by normal actitvy life cycle and you can override onBackpress() (such that some apps give message that press again to exit) and do anything programmatic such that save any data or clear shared preference etc and when you clear it recent list then OS kills the app ,free the resources and there is no guaranty that activity life cycle proceed correctly such that some times onDestroy() not call. so basicly back press is part of our app and application self close by finish() method on other hand clear recent list is part of OS which forcefully kills the app
https://developer.android.com/guide/components/tasks-and-back-stack.html
follow this offical android link for more details
Is there a way to "kill" the app when a user exits with the back
button?
For the sake of curiosity, there is. its System.exit(0). What will happen if you execute this? The VM stops further execution and program(Activity) will be exit/killed right away.
NEVER DO THIS! There is a good chance your app will loose some data if its doing something while you trigger System.exit(0).

Why can't app start from recents if paused too long

I've noticed an issue when I pause my app, say by pressing home, and then bring up the list of recent apps (via the square button) to resume it.
If I resume it shortly after pausing it, it works fine. But if I leave it paused for too long, i.e. a few hours or overnight, then when I touch its window to resume it, it just posts a "failed to start..." toast and removes the app from the list.
I've searched around, but haven't found any info about what would cause this or how to start debugging it.
My theory would be that is has to do with the activity lifecycle. You can find it here:
https://developer.android.com/training/basics/activity-lifecycle/starting.html
When an app is paused, the onPause() function is called, and if the app is paused, and the system needs memory for another app that is running in the foreground, it will stop, or destroy the activity. When you go back into the app, it calls the onCreate() and onStart() functions
I don't know how your app is structured, but my guess is that when the activity is stopped, it is leaving behind some kind of code that needs to reference something that is no longer there, or destroying the referenced thing itself. Then, when onStart() is called, it checks for that thing that is no longer there and crashes.
The best way to fix this is to make sure that each step of the life cycle isn't referencing anything that could possibly be non existent at the time it is called.
When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack.
If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.
You might look up into the Android documentation regarding Tasks and Back Stack and upon some information about Pausing and Resuming an activity. :)
You issue probably related to memory management on android. It looks like android kills you app to release memory so that it can be used by another app. I have an app with very complex layout (The app loads too many images). After some times, when I run the app from recent app list, it always run the app from start. Not from the last state where I left the app. Anywa, nothing to worry about. This is normal behaviour.
As of my concern this is your problem-
you are starting an activity.
then you press home button. and the activity goes to background.
when you start the app after 5 minutes the app starts from the paused activity. but when you start app after 5 hours the app starts from the beginning.
Solution-
This is happening because the activity is being killed in background by the android system.
the application can be killed by calling the ondestroy() method of the activity.
and when the app goes to background it is added to the application stack of the android system. when ever android needs more memory for any operation it takes memory from the last application in the application stack. it kills the last application and takes its memory.
this is the reason your app is being killed after long time. your app is at the last of the stack and android has killed your app to get memory.
you can find more explanation of the process here.
How to solve this problem
this can be solved by bringing your app to front of the stack periodically.
you can run a service from your activity in background so that is stays on top side of stack.
or save the last opened activity in a shared preference and go to this activity when the app starts.

Resume Back Kivy Application even if minimized (not closed) to recent activity

is there a way to resume back a Kivy application to most recent activity even if minimized? i mean when i am in the application ( a compiled application as .apk) and i want to open wifi for example or check other apps, my application closes and i need to restart it again by pressing on it again...so if i am in the middle of an operation in my application (a calculation or an e-mail composition) and i want to check another application ,all my operation would be cancelled if i do so and of course it is a terrible user experience...
The documentation here describes how to enable pause mode, such that your app will be resumed rather than restarted if you leave then open it again.
The main point is to add an on_pause method to your App class, and have it return True.
Note that you are never guaranteed to be able to unpause again (Android itself may kill paused apps if their memory is needed etc.), so you should also use this method to carry out any state saving you need. However, it's unlikely to be a problem for short task switches, and may rarely or never come up in newer devices with more resources.

My Android APP's process has been killed, but still in RunningTask

I have got some programs, need help.
I switch my app(have activity) to background(eg:By Home-Key), and use other APP to kill it.
then check it by Using getRunningTasks() and getRunningAppProcesses(), but i can not understand the result : I can find the app's Activity(TopActivty/BaseActivity)in the RunningTasks, but not in RunningAppProcesses
My problems:
1、Home can i remove all Activity when the process been killed?
2、In this case, how can i restart my APP when click the app-icon?
Thanks
can i remove all Activity when the process been killed?
When your process goes away, your activities, services, threads, and all that go away as well.
However, the "task" remains. This is basically a description of the back stack for your app, and any apps launched from it that were launched into your task versus starting a fresh back stack in another task. This description includes things like your saved instance state (i.e., onSaveInstanceState() Bundle), the Intent that was used to start the activity, etc.
how can i restart my APP when click the app-icon?
If the user is returning to your app via the recent-tasks list, ideally you do not "restart" the app, but return the user to where the user left off.
The developer documentation covers various ways that you can control, somewhat, the behavior of the back stack and your app (or individual activities) as they come back into being when the user returns to one of your tasks.

Categories

Resources