2

There are some things I don't know about android activity lifecycle, don't get me started on fragments!:

Once a method like resume() is called will it proceed to be executed to the very end if finish() or startActivity(new Intent(..)) is called.

 public void onResume(){

      // do some stuff AAA
       ....

      startActivity(new Intent(..));

     // do some more stuff BBBB

      // Do I get this far?

      setResult(RESULT_CODE, intent);


     finish();


     // How about here? Do I get this far?  

    // how do I make sure that everything started in this app up to now is finished off as well? ie that activities started up by this activity are also finished?

}

Btw, this is a similar to top interview question. Will onPause() still get called after finish()? how about onStop()?

Lastly, I would like to know how to finish() not just the MainActivity but all such activities that have started under the application, ie started by MainActivivity, thus going back to the application that originally called my application.

MuayThai
  • 441
  • 1
  • 5
  • 19
  • 2
    Can not you try it by yourself? Is there any problem? Put log into both methods and see result. – Pankaj Kumar Oct 15 '15 at 07:37
  • I want to get at the theory of it. I could do this, but then need to test on different versions of Android etc. I want to understand the why? Also did you see the part about finishing all activities under the app? That's what I am aiming to do and I just realized that I don't really know enough about Android activity lifecycle. For example, yes, I could check if onStop(), and onPause() still get called but really I want to know why or I will just forget it. – MuayThai Oct 15 '15 at 07:38
  • These http://stackoverflow.com/questions/2590947/about-finish-in-android and http://stackoverflow.com/questions/4924071/calling-finish-on-an-android-activity-doesnt-actually-finish may help you. – Pankaj Kumar Oct 15 '15 at 07:41

1 Answers1

3

Yes the code after the call to finish() will be called.

when you call finish() the next function that will be executed is onPause().

If you want to close all activities on the back stack do this:

setResult(RESULT_CLOSE_ALL);
finish();
dsharew
  • 10,377
  • 6
  • 49
  • 75
  • Ok, I recall that there is something that does not get called if you call finish() at some point. Not sure, maybe thats if its done in oncreate(). – MuayThai Oct 15 '15 at 07:42
  • Ok, I have also seen:vv Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); – MuayThai Oct 15 '15 at 07:44
  • yeah great. this will start a new activity clearing the previous ones. then you can only call `finish()` (without `setResult(RESULT_CLOSE_ALL)`) because you have only one activity on the back stack if you always use Intent.FLAG_ACTIVITY_CLEAR_TOP flag to luanch new activity. – dsharew Oct 15 '15 at 07:46
  • However, I wonder than if it also clears that activity of the app that called my app? – MuayThai Oct 15 '15 at 07:47
  • What do you mean by that? do you mean your main activity? – dsharew Oct 15 '15 at 07:49
  • however, the moment you know you are back at FirstActivity might be when you are returned to FirstActivity. Ok, to startActivity(FirstActivity) within FirstActivity? with the CLEAR_TOP? – MuayThai Oct 15 '15 at 07:50
  • to clarify, I have App1.ActivityA calls App2.FirstActivity. My app is App2, and I want to do what I have to do, finish with result to App1.ActivityA. So I am concerned that I not get rid of App1.ActivityA in the process of calling CLEAR_TOP to restart App2.FirstActviity. Everything I do is just in App2, and I have no control over App1. I just want to do some stuff in multiple activities, and then return back so the calling Activity in App1. – MuayThai Oct 15 '15 at 07:51
  • oh.. no problem using this will save you: `Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); `. But why do you want to clear all the activities on the backstack? The Android framework will do that for you when necessary unless you have specific reason. – dsharew Oct 15 '15 at 07:54
  • yes, I do mean my MainActivity which is App2.FirstActivity called. – MuayThai Oct 15 '15 at 07:54
  • I don't know that I need to do it. But I assume that if I opened several activities, then just returning result from FirstActivity will not automatically close the others? I don't know. – MuayThai Oct 15 '15 at 07:55
  • No dont worry about it Android framework will do it nicely for you. Then if it does not agree with any of your scenarios you can tweak it. – dsharew Oct 15 '15 at 07:57
  • according to this answer http://stackoverflow.com/questions/28777449/android-is-onpause-guaranteed-to-be-called-after-finish , if you call `finish()` in `onCreate` then supposedly onPause() of the activity lifecycle is not called – Bhargav Oct 15 '15 at 09:02