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.
Related
Suppose Activity1 starts, and then it starts Activity2. Activity2 performs some task and ends. What
minimal sequence of activity lifecycle methods would be invoked during this? Prefix activity
lifecycle calls with the associated activity. In other words, start your sequence with
Activity1.onCreate()
Try here first https://stackoverflow.com/a/5538421/3720591
This is a easy to find question though, also somewhat demanding lol.
Activity1.onCreate()
Activity1.onStart()
Activity1.onResume()
//intent for Activity2
//finish() method not called here
Activity2.onCreate()
Activity2.onStart()
Activity2.onResume()
//Activity2 performs tasks
//finish() method called
Activity2.onPause()
Activity2.onStop()
Activity2.onDestroy()
Activity1.onResume()
//Activity1 can perform task again
I want my activity the recognize when it is left and a new activity is started so for example when i'll do
startActivity(intent);
It will perform a certain code.
I tried using onPause();
But it only work on leaving the activity manually
Why don't you perform the operation before calling the new startActivity() and go read about the android activity life cycle to understand how it works
EDIT::
if i truly understand what you are saying..then i think you are looking for onStop() method
Look at the Android activity lifecycle here. onPause is called when "another activity comes to the foreground". If I understand your question correctly, it seems like overriding your first activity's onPause method will work. It's called when you add a new activity on the back stack or when you send the app to the background.
For convenience, here's the chart from the link I provided that I refer to for activity lifecycle questions:
I'm talking about programming in android.
In early days I thought that, finish() closes current activity and go back to the previous in Activity stack, and System.exit(0) closes the whole application.
But I was wrong.
I made a small experiment and understood that Both will finish only the current Activity.
The only differences that I could notice is that, in Android 2.3.3
The ActivityResult is propagated back to onActivityResult() using finish(). Whereas onActivityResult() not called for System.exit(0).
But in Android 4.2.2, onActivityResult() is called for both! and Intent was null for exit().
(I tested only in these 2 devices)
There is a time lag when using exit() whereas finish() is faster.(seems like more background operations are there in exit())
So,
what's the difference between two?
In which situations, I can use exit()?
I believe there is something more that I'm missing in between the two methods.
Hope somebody can Explain more and correct me.
Thanks
EDIT UPON REQUEST:
Make an Android application with 2 Activities. Call second Activity from Launcher activity using Intent. Now, inside the second activity, upon a button click, call System.exit(0);.
"The VM stops further execution and program will exit."????(according to documentation)
I see first activity there. Why?
(You are welcome to prove that I'm wrong/ I was right)
Actually there is no difference if you have only one activity. However, if you have several activities on the stack, then:
finish() - finishes the activity where it is called from and you see the previous activity.
System.exit(0) - restarts the app with one fewer activity on the stack. So, if you called ActivityB from ActivityA, and System.exit(0) is called in ActivityB, then the application will be killed and started immediately with only one activity ActivityA
According to android Developer -
finish()
Call this when your activity is done and should be closed. The
ActivityResult is propagated back to whoever launched you via
onActivityResult().
System.exit(0)
The VM stops further execution and program will exit.
According to the documentation, The program will exit.
But it seems a bug in the documentation. In case of a java program, it is correct. But coming to Android, You will see the previous Activity from the stack.
Since Android coding is done using java coding, most of the documentation is same as those for java.
From documentation,
System.exit(0)
The VM stops further execution and program will exit.
For Android aspect, we have to replace the word 'program' with something else. May be Activity or Context.
Sa Qada answer is correct after my testing.
finish will close this activity and back to prevous.
but exit will close current activity too and empty all the activity in freeze and start again the previous activity
Actually there is no difference if you have only one activity.
However, if you have several activities on the stack, then:
finish() - finishes the activity where it is called from and you see
the previous activity. System.exit(0) - restarts the app with one
fewer activity on the stack. So, if you called ActivityB from
ActivityA, and System.exit(0) is called in ActivityB, then the
application will be killed and started immediately with only one
activity ActivityA
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);
.
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()