1

I have 8 Activities in my Android app and I want:

1)Every time I press Back button during my first 7 Activities to go back to my previous Activity(Act1< Act2< Act3< Act4< Act5< Act6< Act7) BUT

2)ONLY when I am in the 8th Activity I want to definitely exit my Android app and go to my phone's Home Screen.I try to do it by overriding onBackPressed method in my 8th Activity (Phone Home Screen<-Act8)

I found an Android implementation in which I insert finish();in every intent of all my 8 Activities but this is not what I want since this way I can't go back to the previous Activity whenever I want(with finish(); every current Activity is removed from back stack).

How will I do it please?

My code so far in my 8th Activity is:

            @Override
            public void onBackPressed()
            {

                   Intent intent = new Intent(Intent.ACTION_MAIN);
                   intent.addCategory(Intent.CATEGORY_HOME);
                   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                   startActivity(intent);
                   finish();

            }

5 Answers5

0

Add a public static boolean to one of your classes that indicates the app is exiting. Set this boolean in activity 8 when you want the app to finish, and have all of your other activities check it in their onResume() and finish immediately if it is true. Make sure the first activity clears it before finishing, or it may still be set the next time the app runs. (Android doesn't necessarily discard the VM when your last activity finishes, so the class and its static members may be reused next time.)

Note that this is the simple way, not the "Android way." Global variables are generally frowned upon, for reasons you can Google. The "correct" way to do this would be to start each activity for result and return a result to onActivityResult(...) that indicates whether the app is exiting.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
  • I think this answer looks by far the best.I haven't done it so far and I try to implement it based on this -similar to you- code(SOLUTION-2) http://stackoverflow.com/questions/8615431/close-all-running-activities-in-an-android-application. I will let you know @Kevin Krumwiede. –  Mar 30 '15 at 23:48
  • I can't reommend using this way due to statics works a bit its own way in Android. In short - statics could be cleared any time so you gonna lost the state of your app in some circumstances you can't even imagine or emulate. – Stan Apr 01 '15 at 09:23
  • @Stan That is completely false. Static fields are not cleared at "any time." Static fields are cleared only when the process is killed and the JVM is destroyed. This may happen any time the app is in the background, which is why it's a bad idea to maintain app state in static fields. But it's actually more common for static fields to *not* be cleared when you expected; i.e., they're still set when the first activity starts because Android reused the JVM from the last run. That's why I recommended explicitly clearing the flag before the last activity finishes. – Kevin Krumwiede Apr 02 '15 at 05:42
  • Ok, statics could be cleared any time the app is on bg and thats why I still wont recommend to use it. This is not about final statics. And you see when something wrong gonna happen due to statics clearing its a huge pain in the @$$ to determine/detect/fix it. You will catch some crashes and you wont be able to reproduce it in most cases. – Stan Apr 02 '15 at 19:20
  • @Stan If an app is in the foreground and you do something that causes the current activity and all activities in the backstack to immediately finish, it's extremely unlikely (if not impossible) that the app will go into the background before this occurs. – Kevin Krumwiede Apr 03 '15 at 05:04
  • What 'bout the Home button? Incoming phone call, skype call, tap on notification etc etc etc? All that causes app to go to bg and asker asks about a proper way to exit app, which means user should do something for that, like tap a Back button and thats why asker shows `onBackPressed()` method implementation. – Stan Apr 06 '15 at 09:42
0

Another way: create a 9th Activity and call it FinishAllActivity or something like that. Make this activity call finish() and then return in its onCreate().

In onBackPressed() in Activity 8, start FinishAllActivity using the FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK flags (see this question for more details). Activities 1-8 will be removed from the stack, then the 9th Activity will start and immediately terminate and your task stack is clear. When you reopen the app it should start from Activity 1.

The advantage of doing it this way is that you don't have to modify Activities 1-7.

Community
  • 1
  • 1
samgak
  • 23,944
  • 4
  • 60
  • 82
  • Not the optimal way of fulfilling so but it worked. By the way I want to clarify that Android has its own way of terminating its applications.In this version of my app I accepted this answer since it works, but the optimal way of taking advantage of Android's workaround is always sending our apps in the background.I strongly suggest doing so.That will be my prompt app update.Thank you all and especially you @samgak ! –  Apr 06 '15 at 08:45
0

You can implement a broadcast receiver and have each of your Activities that you want to close call finish() when they receive the broadcast (which will be sent from your last activity). I would imagine you'd need to have your broadcast receiver class be either an anonymous inner class or a private class within your activity(s) so that you can easily access your enclosing Activity's finish method.

Here's a good example of broadcast receivers: http://www.tutorialspoint.com/android/android_broadcast_receivers.htm

Look at the custom intents section.

Doing it this way is a loosely coupled way to implement what you are looking to do.

Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37
  • Activities on the backstack may be destroyed, in which case their receivers would not be registered. A well-behaved app works perfectly with "don't keep activities" developer option checked. – Kevin Krumwiede Apr 03 '15 at 05:08
0

use FLAG_ACTIVITY_CLEAR_TOP Flag in your intent like below example.

 Intent intent = new Intent(getApplicationContext(),FirstActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  intent.putExtra("EXIT", true);
  startActivity(intent);

in your first activity check below condition.

if (getIntent().getBooleanExtra("EXIT", false)) {
Intent intent = new Intent(Intent.ACTION_MAIN);
               intent.addCategory(Intent.CATEGORY_HOME);
               startActivity(intent);
               finish();
}

here FLAG_ACTIVITY_CLEAR_TOP work like below example

consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

so here you have to call D is your last activity and A is your first activity.

Community
  • 1
  • 1
Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41
-1

This way you are finishing your 8th Activity returning to your 7th Activity and same time you are like emulating a pressing of Home button on a device. When you rerun your app it will appear with 7th Activity on a screen. If you wish to see the 8th Activity in this case then just remove the finish() method. If you wish next time your app to start with 1st Activity then you should finish all the activities from 8 to 2nd but not the 1st. Also you could launch your 1st Activity adding a FLAG NEW_TASK or some other flags.
UPDATE
My advise (for the quick result without changing the workflow) is to use startActivityForResult() to start all activities in chain. When user exits the app just return a special parameter using setActivityResult() to get all nested activities know about user's choice making all nested Activities to run finish(). This way all your 8 activities will be finished properly.

Stan
  • 6,511
  • 8
  • 55
  • 87
  • I don't want to finish all activities from 8th to the 2nd leaving the first Activity because my app is resource consuming and this way it wont exit but it will permanently consume more and more RAM instead. I just want to exit the app and rerun it from the 1st Activity. –  Mar 30 '15 at 22:20