Is finish always scheduled before startActivity? - android

startActivity(newActivity);
finish();
Assuming I'm calling it like above. Both calls are scheduled to happen on the UI thread after the end of the invoking method. However, is there a particular order in the scheduling? Is finish always scheduled before startActivity? or vice versa?

When calling finish() on an activity, the method onDestroy() is executed this method can do things like:
Dimiss any dialogs(search dialogs) that activity was managing.
Close any cursors the activity was managing.
And the activity is removed from the stack.
And calling startActivity(newActivity) creates and puts a new View on the Top.
Thus,if order is
startActivity(newActivity);
finish();
Then first newActivity is displayed and old one is detroyed.
But,if order is
finish();
startActivity(newActivity);
Then first the existing activity is destroyed and new one is created and displayed.
So, if we have lots of things to do in onDestroy()(like storing some datas) then calling startActivity() then finish() will be a good thing to do.Thus, the order depends on what we call first.

You should always Start Before You Finish
Hence the most desirable method call order would be,
startActivity(NewActivity);
finish();
Mark Murphy wrote an excellent article about this.

It will work in both the cases whether you write finish () before or after the start activity()

So basically the order of the scheduling depends on the order of the calls. If you call finish() first, it will be added into the queue followed by startActivity(). Same goes for the other way around. Depending on how your implementation goes, if you require a specific order in the scheduling, simply call the functions in the order that you want the scheduling to occur.

It should work is both cases.

Related

Finish Theme.Transluscent.NoTitleBar?

I have an android activity with Theme=Theme.Transluscent.NoTitleBar.
I want to use finish this kind of activity using finish() but it doesnt work. As it is an invisible activity it goes to onPause State rather than onDestroy state.
How can I overcome this problem as it uses unnecessary memory.
Thank YOU !!
There are many threads which explain this. When you are calling finish(), Android will let your code in the specific block after the finish() call execute, and that is why the Toast message appears. A simple return statement after the finish() call is the solution. Taken from answer on question:
Android Help! I want to completely finish my activity, and after activity.finish no further code will execute?
Other relevant ones are:
Calling finish() on an Android activity doesn't actually finish
about finish() in android
Hope this helps.
you can use System.exit(0); but as stated it is not a good approach. but however you can use this approach to clear the previous activity intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
.

What does this.finish() really do? Does it stop my code running?

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()

Difference Between Synchronous Activities ans ASynchronous Activities

Please tell me the Difference Between Synchronous Activities ans ASynchronous Activities..
and also about that
-startActivity() is Synchronous or Asynchronous
-startActivityForResult() is Synchronous or Asynchronous
and why..
From first principles, synchronous activities means that Activity A operation will depend on activity B eg(coordinating to work with a shared param) Asynchronous activities means that the operation of two activities are totally disconnected.
According to my general concept both startActvity() and startActivityForResult() are asynchronus as in the synchronus Activity the current activity will depend on the new activity.
For example :
suppose in current actvity:
Intent i=new Intent(First.this,Second.class);
startAcivity(i); //or startActivityForResult(i,0);
Log.v("First","Activity");
Now for any activity to be synchronus the later part of the current acivity (once the new activity is started) must be executed after the onCreate method of the new actvity(here Second class).
But generally this never happens. I mean the later part of the current Activity( once a new acivity is started by calling startActivity() or StartResultForActivity()) is never depended on the onCreate method of the New activity.
for more you can go through these links :
What is the mechanism behind startActivityForResult() in Android?
http://osdir.com/ml/Android-Developers/2009-12/msg04249.html
Hope from the above example you understood exactly what startActivity() & StartResultForActivity() are and what is synchronus & asynchronus Actvity is....:)

Question about Intent, android

I am confused, and need to get my concepts straight.
After executing the last statement, which function is called, in MapsActivity? is it onResume? and under which function (onResume()?) should i put getExtra()?
Log.i("onMenuAnimate", "Attempting to animate to:");
Intent intent = new Intent(SearchDB.this, MapsActivity.class);
intent.putExtra("com.gpsdroid.SearchDB.Lat", nameLatitude.getText());
intent.putExtra("com.gpsdroid.SearchDB.Long", nameLatitude.getText());
SearchDB.this.startActivity(intent);
Take some time to read up on Activity Life cycle; trust me it will help you a lot.
Under the given circumstances, when you call startActivity(..), MapsActivity will be first started by the Activity Manager. In an activity's life cycle, onCreate(..) is called whenever an activity is first created. So this could be one of the places that you can call getExtra().
AFAICT, you can call getIntent.getXXXExtra() in any of the Life Cycle Methods. The answer regarding which of the life cycle methods to choose depends on what is being passed and where/when the information is to be used.
after this statement the next activity which is going to be called.then whenever the back button is pressed the a\first activity will be resumed. the code you want to execute you should put it in overrided method onResume.

Finish sub activities programmatically

I have an activity that launches another activity with startActivityForResult method. I would like to terminate the called one programmatically but I don't know how to do this since in onActivityResult() method I have no information about the called activity and I cannot call finish() on it. How can I achieve this?
Thanks
The launched Activity can finish self:
setResult(RESULT_OK);
finish();
Try finishActivity(requestCode). According to the documentation it lets you finish an activity previously started with startActivityForResult. And if there are multiple such activities with the same request code, all will be finished.
Note: I haven't actually tried this myself, but that's what the documentation says! Experiment with that, see if it does what you want.
At the moment you call startActivityForResult your Activity will be closed or paused and the new activity is started. The only one that can finish the new activity is the new activity.
You could start a background task and let this background task somehow notify your activity the activity could now finish itself.
I don't know if a Handler created in Activity A and passed to a thread will remain valid if Activity A is paused and Activity B is active. But I would assume this works because both of the Activities are running in the same thread therefore they should share the same message queue.
This thing just bit me, so I thought I'll add a comment here:
if(readyToFinish()){
finish()
}
thisCodeWillBeExecuted()
My experience is that all your code in the stacktrace gets executed, before
the activity is finished. The documentation is not ideal at this point.

Categories

Resources