I am facing some problems so as a conclusion can I get how to know current Activity is finished or not in onSavedInstanceState() ?
Is there any possible solutions then let me know.
In your Activity, you can call isFinishing() (which can tell you if the Activity is in the process of finishing) or isDestroyed() (which can tell you if the Activity is invalid). Please note that isDestroyed can only be called if you are using API 17 or above.
For the official references, you can see here: http://developer.android.com/reference/android/app/Activity.html#isDestroyed()
http://developer.android.com/reference/android/app/Activity.html#isFinishing()
Check for onDestroy() Method, if its gets called or not.
According to android docs
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.
If your onDestroy method of activity gets called your activity will be finished and no instance will be saved in the back stack.
Related
I am making an Activity that logs a message when the activity is destroyed due to orientation change.
What I want to do is to call that Log.d() UPON the moment the activity is destroyed.
In other words, I don't want to call it by checking savedInstanceState==null after the activity is recreated.
Is there a way to know why the activity is destroyed before I reach onDestroy()? Thanks
You can use isChangingConfigurations() from the docs:
Check to see whether this activity is in the process of being destroyed in order to be recreated with a new configuration.
Docs available here
If I understood correctly you want to log something before destroying activity.
According to the lyfecyle activity diagram you should do that at this point.
In your activity maybe you have to overwrite onDestroy method and call log before the super call.
#Override
public void onDestroy() {
//log
Log.d()
super.onDestroy();
}
EDIT:
Juan is right, as the docs mentions:
"There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away."
https://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29
I wrote a game, and in logs from market i sometimes see following pattern:
06:02:13:835|INFO|1|MainActivity|MainActivity#2.OnCreate
06:02:13:932|INFO|1|MainActivity|MainActivity#2.OnStart
06:02:14:010|INFO|1|MainActivity|MainActivity#2.OnResume
...
06:09:27:688|INFO|1|MainActivity|MainActivity#2.OnPause
06:09:28:895|INFO|1|MainActivity|MainActivity#3.OnCreate
06:09:29:159|INFO|1|MainActivity|MainActivity#3.OnStart
06:09:29:319|INFO|1|MainActivity|MainActivity#3.OnResume
06:09:29:551|INFO|1|MainActivity|MainActivity#2.OnStop
06:09:29:596|INFO|1|MainActivity|MainActivity#2.OnDestroy
MainActivity instance #3 is created and started before MainActivity instance #2 is destroyed. What does this pattern mean ? Why does new instance start before previous has been destroyed ?
Obviously i don't create activity by hand. Users simply start game by tapping icon(at least i hope so).
Activity has singleTask launch mode.
Thank you!
The onDestroy callback is not guaranteed to be called. From the docs:
onDestroy() = The final call you receive before your activity is
destroyed. This can happen either because the activity is finishing
(someone called finish() on it, or because the system is temporarily
destroying this instance of the activity to save space. You can
distinguish between these two scenarios with the isFinishing() method.
Suppose your application has two activities. Then this would be the life cycle
onDestroy will be called if you explicitly call finish(); yourself or like the example if you press Back button because pressing back key actually provokes finish() method on your activity, and it causes your activity to be paused->stopped->destroyed
I have Activity A and I am calling Activity B from Activity A using setResultForActivity.
Now in Activity B when I press Done button I am firing finish() and it returns to
Activity A and it return down to onActivityResult. Now the issue is after when I fired finish() in Activity B , Activity A's onCreate doesn't get called and thats why
some of the custom listeners in my ListView isn't working , it seems that they are not bind.
so the whole activity respond pretty weirdly , can anyone has solution to this ?
Why a fourth answer? Because in my view, the others aren't fully correct.
Truth is, Activity A could have been destroyed in the meantime, or not. This depends on whether Android needs memory or not. So it is possible that Activity A´s onCreate() is called (along with the other lifecycle callbacks), or not. In the latter case, onActivityResult() is called before onResume().
While for configuration changes, the most efficient way to preserve the Activity's state is via nonConfigurationState, if you want to prepare for a restart of your Activity after it has been destroyed, you can use the InstanceState mechanism, because while Android destroys your Activity A, it will keep its intent and saved instance state to re-crearte it.
This stresses the absolute necessity to place initialization exactly in the callback where it belongs.
To test whether your Activity logic works regardless of whether Android destroyed it or not, you can use the DevTools setting "Development Settings" -> "Immediately destroy activities". The DevTools app is available on AVDs and can also be downloaded from Google Play.
Just place your onCreate() stuff in onResume() of Activity A except setContentView().
Just have a read on Android Activity Lifecycle : http://developer.android.com/training/basics/activity-lifecycle/stopping.html. onCreate() is only called when the activity is first created. You can do your list thing in the onResume() method.
Activity A's onCreate won't get called because the activity has not been destroyed. When an Activity regains focus from another activity, it's onStart and onResume get called, so I would put your bound listeners in those. They will also be called when onCreate is normally called.
After your child activity is finished() it return to execute onActivityResult which is in your case in Activity A. The onCreate method is not supposed and does not get called when killing of you sub-activity, a.k.a Activity B.
Please post some source code for us to work on and I will improve my answer! :)
I know that finish() returns your activity back to your previous activity. Now I am curious if we are able to accomplish the opposite meaning forwarding back to the next activity that you backed off without doing an Intent. This is just a curiosity question.
Is this possible?
No. The "next activity that you backed off without doing an Intent" was destroyed by a call to finish() when the user pressed BACK, so you cannot return to it.
No, that is not possible, once you run finish() (or press back) on an activity it will be poped from the activity stack and al its content garbage collected, only way to reach if again is by starting it with and intent.
Short answer: No, because The activity that finish()ed was destroyed.
Long answer: From the Activity Documentation
onDestroy() - The final call you receive before your activity is
destroyed. This can happen either because the activity is finishing
(someone called finish() on it, or because the system is temporarily
destroying this instance of the activity to save space.
Calling finish() doesn't actually guarantee immediate GC on the activity, but it will be made eligible soon after a call to finish(). You can assume that anything in the activity instance is gone if it wasn't persisted. Don't hold references to Activities that Android says should be killable, per Activity Lifecycle and Avoiding Memory Leaks, this is not a way to get around this, and is a Bad Idea(tm).
You could override OnDestroy() and check isFinishing() if you'd like to store the activity history in your application, so that you can manually implement something like "forward" functionality, but in general it's better practice to do something like that in onSaveInstanceState().
NO,because once you call the finish() method,it will destroy that corresponding activity.The only way to accompanish your task is by using an intent.
I have an activity where the first thing it does is start another activity for result. when the result comes back, it should process it, then finish. I have the startActivityForResult() call in onCreate().
What I see is that sometimes when I return from the target activity i started, onCreate() in my activity is called again. this of course re-starts the target activity a second time.
This makes sense and I understand why this is the case, but I don't understand the correct pattern for what I'm trying to achieve. When I return from the activity i started, i don't want to re-start the target activity again obviously ... I just want to run onActivityResult() and finish.
I read where someone suggested setting a state preference, but that seems like a good source for bugs, for example, if it got stuck in the wrong state.
Any thoughts?
The key is to start the target activity in onResume(), not onCreate(). From the javadocs on onActivityResult(),
You will receive this call immediately
before onResume() when your activity
is re-starting.
In other words, you can be assured that onActivityResult() is called before onResume() ... So for example, set a flag that says "don't start target activity this time" in onActivityResult() so when onResume() is subsequently called, you can avoid re-starting the target activity.