There are two activities in my example, MainActiviy and EmptyActivity. In the MainActiviy, it create a notification, which target is the EmptyActivity. Well, EmptyActivity is very simple, just finish itself in its onCreate.
i added the trace in the MainActiviy's onDestroy and onBackPressed
I have invoked the finish() method in the onBackPress
The problem is:
open the application, and press back key, the log is printed in the logcat, means the MainActiviy's onDestroyed is invoked.
if open the application, and press the notification from the status bar, and then press back key, then no log in the onDestroy is printed
Strangely, if reopen the application, then the log in the onDestroy is printed..
Has anyone known why this happened. Thanks
i uploaded the example to the google drive
Test
why minus my question!!!
The link is different from mine !!
onDestroy() is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.
i have invoked finish() method!! My question is "why onDestroy() is not invoked after finish()"
Related
I am trying to mantain a log , when exits the applicaiton. I have used this code :
public void onDestroy() {
super.onDestroy();
Log.d("D", "Destroyed");
}
But this only works when I press the Back button. When I press Home button , the application Pauses , and If I close this application from task manager , then the onDestroy function is not called. How to handle this ?
Any idea ?
You can't handle the closing of application from task manager. In this case you're killing the app and onDestroy isn't called. You should make all clean up in onPause
You can do your stuff in onPause() method.
In your case:
If End Process is used from Process list in task manager, then nothing is called in application, the application is simply terminated.
If End Task is used from Applications list, then WM_CLOSE is sent to the window, which in turn allows application to do the cleanup.
onDestroy() is called when an activity finishes its life cycle. It is also called once in the lifecycle of an activity.
The OS decides when things "go away." The onDestroy is there to let your app have a final chance to clean things up before the activity does get destroyed.
From the Android Developer Guide:
There are a few scenarios in which your activity is destroyed due to
normal app behavior, such as when the user presses the Back button or
your activity signals its own destruction by calling finish(). 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.
When you switch between apps by pressing the home button, Android pauses the activity and resumes it when you return to the activity.
For the most part, the OS decides when to quit an application so it wouldn't make sense for you to log when an activity is destroyed. I would suggest overriding the onPause() or the onStop() method
When I run my app on the debugger, I get the main thread and 3 binder threads.
On a button click I call Activity.finish(), which looks like it ends the activity as the UI closes and goes back to the home screen.
However, in the debugger, it still shows the main thread and 3 binder threads as "(running)".
I am baffled to why this is happening. Even more so, it is causing my app to call Activity.onResume() when I run it again after exiting the app.
I currently override these methods in the Activity, but I call the appropriate super functions in each one:
onDestroy()
onPause()
onResume()
onSaveInstanceState()
Any help or advice regarding this is much appreciated!
You don't control when your app leaves main memory, the OS does. Look closely at Activity.finish...
Call this when your activity is done
and should be closed. The
ActivityResult is propagated back to
whoever launched you via
onActivityResult().
Note that is says nothing about memory. As to calling Activity.onResume, that's exactly what you would expect for the lifecycle; remeber that onResume is not just called after a resume but even when the app is first launched after onCreate.
While not exactly what you asked I suggest you read this article about exit buttons which goes on to say something very important
[Activity.finish] is exactly equivalent to hitting the back button.
I'm having a couple of problems with an alarm app I am developing.
The first thing that I think is a bit weird is that when an alarm goes of and wakes the phone up. These things happend.
oncreate
onresume
onpause
onresume
Why are they run in that order? The last two should not be called? And this is what's causes me big trouble, because when i press home or back on the phone, onPause is run, which I want to call finish() from. And that part works as it should, but that does not work when the phone wakes upp from sleep bacause of the onPause call...
Any ideas?
I also have a similar problem: i create an Activity and a Service in the background. When the service receive some data from the net it will bring-up the activity.
Everything works fine until the activity in in the onStop phase and the screen goes black.
When I have this situation and I request the activity to restart, I have:
onRestart
onStart
onResume
onPause
onNewIntent
onResume
As you can see I have an onResume, onPause and another onResume and the activity came to the user.
Seems that is because we use a singleTop or singleInstance Activity (maybe also appear on singleTask). The problem is that when you send an Intent to an already created singleTop Activity, your new intent active the onNewIntent() method. from the API you can see that:
An activity will always be paused
before receiving a new intent, so you
can count on onResume() being called
after this method.
so maybe this is why you receive onResume-onPause-onResume calls.
However this is a strange behavior!
I would suggest you look at how the official alarm application handles the events:
https://android.googlesource.com/platform/packages/apps/DeskClock
Do you really want to call finish() in onPause()? onPause() is called when the current activity is not in the foreground any more. Maybe you want to call finish() in your onStop() method instead. See the Activity lifecycle for details.
In an if statement, I have a finish() because I don't want the control to go any further. But, the app exits, but as I see from the logcat, the control goes beyond finish().
Maybe that's also the reason why a toast supposed to be displayed and some other GUI actions before finish() are not taking place?
Please advice.
onPause and onDestroy will be called after finish
so i've been trying to get my application to run an Activity via an intent and it works fine, when i then assign the finish(); method, it returns to the activity that called it. The only thing i don't understand is that i'm not sure if the callee Activity is put onPause while the called Activity is in-front. I've tried to setup a toast message in the onPause() method of the callee Acitivty but it won't appear.
I first tried to call the second Activity with startActivity(intentname) and then a finish() method on the first Acitivty, i then tried to use the startActivityForResult() (even though i don't really need to recieve any information from the called Activity) method and closed it with onActivityResult().
I can't find any information about the side-effects that these Activity methods has on a Activity that's calling another. So i'm wondering if anybody could help me out ?
//Thx in advance
According to the documentation for Activity, the onPause() lifecycle method WILL be called when another Activity is put in front of it.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If the called Activity is is semi-transparent, then onStop() will also be called, but if your initial Activity is not visible at all, onStop() will not be called.
It is also of worth to note that when you call finish() on the called Activity, the onResume() will be called on the caller (and onStart(), assuming onStop() was also called)
To quickly answer your question: if activity A starts activity B, then A's onPause method is run. I think there might be an exception if B isn't full screen, but that's only a tentative memory from something I read in the documentation a while ago.
As for why your toast wasn't showing - did you remember to .show() it? I always used to forget to do that. Toasts can also get missed if they're triggered just as the activity is pausing, since its context goes away. There's a much easier way to test it - just use the Log method. For example, Log.d("My app name", "onPause was just triggered"); The purpose of the "My app name" string is to let you filter by it in LogCat. If you don't know how to display LogCat, and assuming you're using Eclipse, see this answer to another question.
got it to work , was a bit confused of the purpose with onResume(), i was suppose to decleare a onActivityResult() in the first Activity so that the second Activity would return to it right after finish()