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()
Related
My team wrote a complex LBS/chat App, there is a MainActivity as main displaying activity.
When this MainActivity started, "onCreate" is called the first time.
when I switch to other several Apps and go back to my App's MainActivity, "onCreate" is called second time.
But "onDestroy" of MainActivity has never been called.
Some pointers(Singleton/Global, not belong to any activity) will be "auto" set to null out of my control, it causes my app problem.
Does some module/class hold a reference to MainActivity or some other reasons?
I need help -_-!
Thanks to all guys who can give me some tips.
Thanks!
OnCreate is call only once in activity circle life.
If your application was in background and return to foreground, the only reason that on create call again is because OS kill this activity.
you can save data with onSaveInstanceState().
when you start activity this functions be called: onCreate() , onStart() , onResume() and when you going to another activity, onPause() called.
If you want to call onDestroy() to your app you must call this.finish(); but, when you call this.finish() you must handle onBackPress() in your app.
I hope this useful to you
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'm a bit hazy about what exactly this.finish() does. Specifically, I just wrote the following lines of code in an activity:
this.finish();
Globals gs = (Globals) getApplication();
gs.MainActivity.finish();
The code is meant to close the current activity and also close the core activity of the app. And it works great. However, I got wondering... obviously the current activity isn't quite ended after the first line is executed. And what if I was to call this.finish() and then start on some complicated computation?
My question is: When I call this.finish(), when exactly does my Activity get taken down?
Whatever method called finish() will run all the way through before the finish() method actually starts. So, to answer your question, after the calling method finishes then your activity will run its finish method.
If you don't want the method to continue then you can add a return statement after finish
I'm a bit hazy about what exactly this.finish() does
Calling finish() basically just notifies the Android OS that your activity is ready to be destroyed. Android will then (whenever its ready) call onPause() on the activity and proceed to destroy it (no guarentee onDestroy() will be called), via the activity lifecycle. In general, you probably should not be doing any more execution after you call finish(). According to the Android docs, you should call finish() when you are ready to close the activity.
when exactly does my Activity get taken down?
I am guessing your activity will simply be added to some destroy queue. During this time you might be able to continue executing until the OS destroys it. I believe you are for sure allowed to finish executing the method from which finish() was called.
Activity.finish() will not stop the current activity until the method is completely executed so to skip the remaining part of the code, you may use a return; use it with some condition to validate your skip.
if ( condition = true ) {
this.finish();
return;
}
Chris, I am no expert, but at the answer here about finish() in android is basically what codeMagic just said. The link is valuable because of the discussion regarding onStop() and onDestroy()
Here is the scenario:
I have two Activities. Lets name them Activity A and Activity B.
Say Activity A is open. Now, when I go and open Activity B, Activity A is closed because the onStop() method is called.
Now, when I flip back to Activity A, the onCreate() method is called, but I want the onRestart() method called instead. How do I do this?
You cannot influence the livecycle of your app like that. There should be no reason to rely on onRestart(). If you use onStart() it will always be called no matter if the Android OS killed the app process in the background.
Check out this doc for further information:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Damn beat me to it but here goes anyway
According to the Activity Lifecycle onCreate() is called again if the Activity was removed from memory because the OS deemed that another app needed the memory. In this case, you can't ensure that onRestart() will always be called for your Activity.
Like already stated you must find a different way of achieving your goal by using the other Lifecycle methods such as onStart() or onResume
I'm not sure if it fits your needs, I had to do an update service that starts the first time I open ActivityA (main Activity) and stops when exiting from ActivityA (not returning back from ActivityB),
I've placed the "start code" in onCreate() when savedInstanceState is null and the "stop code" in onDestroy() if isFinishing() is true
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.