ANDROID activity force stop on restart - android

I've looked through a lot of other threads about this but haven't had any luck yet with their solutions.
When I launch my app for the first time, I noticed that onResume is being called after onCreate. (not sure why)
My problem is when I try to run the application for the second time. Neither onCreate nor onResume get called, it just sits there.
ALso, I have these setting set in my manifest:
android:clearTaskOnLaunch="true"
android:screenOrientation="landscape"
android:noHistory="true"
If I ever want to run my app more than once, I have to go into settings->applications->app-name->force-close
(I don't have access to look for any loops or bad bugs that might be causing the sticking because I'm using a library with it's own threads.)
Is there a way I can fix this?

Call Process.sendSignal(Process.myPid(), Process.SIGNAL_KILL); in your onDestroy method. This is not good but the best solution for this problem.

This is the way android manages the activities. If you want your activity go through whole life cycle, call explicitly its finish() method.

Related

onCreate() and onDestroy() NOT symmetric?

I am learning android and came up with what I think as a weird observation:
based on the diagram and description on https://developer.android.com/guide/components/activities/activity-lifecycle
It seems the two callbacks onCreate and onDestroy are not symmetric.
It seems that when an app loses its focus or be put to the background, the system could kill the app without calling onDestroy. Later on, when the app is up again, onCreate is called.
I tried to test this, but couldn't figure out how to simulate the situation when the system would kill the app because of memory issues. I tried to open a lot of apps on my phone, the test app was never killed :)
Let's say it does happen in some cases. Does that mean you could have onCreate be called more than onDestroy, which could potentially cause memory leaks if you happen to acquire resources in onCreate and release the onDestroy? If my observation is true, any best practices out there to solve the resource release issues?
Thank you.
The documentation can really explain this better.
However, in short, onDestroy() will be called if an Activity is ended with finish() or Android needs the resources that your app is using.
I will typically not use onDestroy() to manage resources. In fact, I do not think I have ever used onDestroy() in any app I have written.
I will use onPause() to ensure that resources are gone in a timely fashion. You will only really need to do that for resources that are registered (like BroadcastReceiver). Stopping repeating Handler messages. Things like that.
i'm not sure but i think there is a way to simulate system kill app with adb command
https://possiblemobile.com/2017/10/android-testing-app-killed-background/
I tried to test this, but couldn't figure out how to simulate the situation when the system would kill the app because of memory issues.
Just use Settings->Developer options->Dont't keep activities mode to get onDestroy all the time. Also use Background process limit at the same menu.
Loose call of onDestroy is extreme situation. System calls onDestroy every time activity destroyed, even in not enough memory case.
Thanks everyone. With tips from #Igor, I was able to test and prove the points mentioned by #Knossos: OnDestroy is called as soon as the app loses the focus. Which means onCreate and onDestroy are still symmetric, as far as I am aware. Which also means the diagram on the android documentation site (https://developer.android.com/guide/components/activities/activity-lifecycle) needs a little improvement.
Thanks again for everyone's input. As to the best practices for resource management, now that I think more of it, it deserves a whole lot of in-depth discussions, as many factors could contribute to the complexity: the types of resources, the system overhead of getting and releasing them, etc...

onCreate() called while Activity is stopped (but not destroyed). Only after installation

I'm working on an app that targets Api 19, and that is basically a Processing sketch.
The problem I'm facing is that the first time my app is run, right after installing it, it works well until the user sends it to the background. Then, if they click on the app icon again, onCreate() is called but the activity is not destroyed or restarted. Some variables change and that produces strange behaviours.
This happens ONLY the first time the app is used. After force closing it, this behaviour won't happen ever again (as far as I've tested). And this does not happen when launching the app from Eclipse either.
Summarising, this is what happens after the first force close (and what I deem correct):
Activity is running.
Activity is sent to back via home button
onPause()
We click on the app icon again
onResume()
And this is what happens -ONLY- the first time the app is run after installation:
Activity is running.
Activity is sent to back via home button
onPause()
We click on the app icon again
onCreate() <-- !! note no onDestroy()
onResume()
I wonder if the fact that I'm using Immersive Mode has anything to do with this, but changing the Api target version to 10, removing Immersive mode or testing on old devices do not help. I have, of course, used android:configChanges="orientation|keyboardHidden|screenSize" on my manifest.
Does anyone have a clue on what might be causing this? Is this a common issue or should I look for a bug in my code? Maybe a Processing bug?
Thanks in advance for any clue. I hope this is the right way to ask about this issue. This is my first post.
Update:
My explanation is not very accurate, but apparently there is a bug report for this. The problem is much better explained here: https://code.google.com/p/android/issues/detail?id=26658
Unfortunately, I can't get the proposed solutions to work, using this in onCreate() causes my app to either close or crash:
if (!isTaskRoot()) {
finish();
return;
}
Ok, this is how I solved it, in case anyone else bumps into this wall.
This might affect especially people coming from the Processing Developing Environment, as it converts a "Processing sketch" into the only activity of the project.
The original problem (Android managing apps in a different -wrong?- way when launching them from the Package Installer) is well explained here: https://code.google.com/p/android/issues/detail?id=26658
Solutions posted there might solve most cases,but if -like me- your launcher activity is the one that carries out all the work, you will need to create a specific launcher activity that just starts the main one... and commits suicide when the Android bug happens.
Add this bit to the onCreate() method of the launcher activity:
if (!isTaskRoot()) {
finish();
return;
}
I hope this helps.
This looks like a valid application lifecycle, you put your app to background, android is then allowed to destroy your app. onDestroy is not guaranteed to be called, you have to be prepared for that, as you say onPause is called so you can use it. Why it happens only once after install is hard to explain, but in my opinion you should not actually care about it and prepare your app to be killed any time it is in background.
In above diagram may OnDestroy method never called because other application need memory.
onStop() is the last method which is guarantied to be called. After this method Android is allowed to kill your activity at any time. Check out the table in the activity's lifecycle table. There is a "Killable" column describing the time, when activity can be killed.
If you don't use static variables and you initialize everything properly in onCreate(), then you should have no issues with this behavior. Just never expect onDestroy() to be called.

How can I know an activity is going to be killed by OS?

I know I can use isFinishing() in onPause() to know whether an activity is going to be killed by finish().
Then, how can I know an activity is going to be killed by OS temporarily due to low memory?
Thanks.
Per the docs, onDestroy should be called right before the Activity is destroyed, regardless of the reason. If the finish was requested, isFinishing will return true. So if it is false, you can assume that the system needed to finish.
However, as the docs also say
Note: do not count on this method
being called as a place for saving
data!
In general, you cannot guarantee that your Activity will be killed nicely. Things like task killers mess with the lifecycle.
Use onPause or onSaveInstanceState to save things properly.
You cannot.
It's possible your activity could go away without the rest of your app going way, in this case onDestroy would be called. However it's also possible that your whole app is going to get killed at once, this like a kill -9 in unix. Your app cannot run any code at this time, it's killed instantly and without warning.
To handle this properly, you want to design your app to save all vital information to disk in onPause and be ready to retrieve it later in onCreate if needed.

Completely finish the actitvity

In made the android app. I installed it in my mobile. When I press back button on my mobile it comes out of the app. But it is running in the background. So, how can I finish the activity completely? I wrote the code onkeydown event also. Even it is running in background.
You should override the onStop() method on your activity and in call the finish() method from it. Don't forget to call super.onStop() at the beginning of the onStop() method.
As far as I know it's not running on the background, android saves the state of the activity and when it comes back, it restores your activity with the same state it was.
You can call Activity.finish() to kill of an activity completely.
Be aware that this it not something your should normally do, as Android really want to manage the activity life cycle for you, as mentioned by Sheikh Aman.
Hi see the following link in that also same question is available.Finish the android application completely
I will keep running in the background. Android decides when to kill your activity.
By the way, How do you ensure that it is running and "working" (is not frozen) ??
In your onKeyDown() handler you should add this:
finish();
This causes the activity to be closed.
Call this:
android.os.Process.killProcess(android.os.Process.myPid());
It's odd, but only this call guarantees that you will kill your application. Due to specific Android life-cycling neither finish() nor any other methods doesn't kill application.

Destroy Android application

I have developed an application which is working fine.
In that i have used some static variables and also set Application level variables.
My problem is that even after setting finish() on each activity, application is showing in Running mode.
After closing the application, when i start the application after sometime, it will set the last changes.
How can i destroy my application?
I think the correct answer is "you shouldn't".
It doesn't matter if your app is still running after the user stops using it. Android will kill it when/if it needs to.
Your app most likely shouldn't even have an exit button.
I have checked that application does not release the resources.
So, what i have done is :
Process.killProcess(Process.myPid());
It will kill the application.
It helps me a lot.
call
System.exit(0);
from within
OnDestroy()
And yes, you shouldn't.
I hope you have called onDestroy for your activity.
Secondly, are you saving your preference some where else??
Please go through these methods used for the destroying the activities
setResult() finish() and onActivityresult()
After calling setResult() call finish() for the activity that you want to finish & then in it's parent activity override onActivityResult() in this you can finished your application.
The solution given by oli is right android will destroy your application running. But I want add, only if it is lacking in system resources.
For more help please go through following link.
http://developer.android.com/reference/android/app/Activity.html

Categories

Resources