2

Activity A -> Activity B -> Activity C -> Activity D. Pressing back in Activity D should navigate user out of the application, whereas Back should work normally for Activity C, B and A. i.e. C -> back -> B -> back A -> back -> exit.

I tried settings flags with Activity.FLAG_ACITIVITY_CLEAR_TOP, etc.. with different combination. Nothing seems to work. Minimum API level 7.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110

4 Answers4

2

For #1 Pressing back in Activity D should navigate user out of the application

In ActivityD, you can override onBackPressed or onKeyDown method and then start ActivityA with flag FLAG_ACTIVITY_CLEAR_TOP with an Exit flag(via putExtra method). In ActivityA, you can get that Exit flag value and call finish() if Exit flag is set true.

In Activity A's onCreate method you will have to do is

if (getIntent().getBooleanExtra("EXIT", false)) {
 finish();
}

In ActivityD's onBackPressed or onKeyPressed method,

intent = (this, ActivityA.class);   
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   intent.putExtra("EXIT", true);
   startActivity(intent); 

For #2 Exit from ActivityA

You can simply call finish() method on onBackPressed or onKeyPressed.

Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
  • This will simply the application to show Activity C. Seems like catching the boolean flag and calling finish on Activity A will then cause the application to look for the last Activity on stack, i.e. Activity C. – user1682701 Sep 20 '12 at 07:16
  • Thanks, I had to do some other changes as well. But your suggestion worked out. – user1682701 Sep 20 '12 at 10:21
  • The OP used the word ***properly*** –  Apr 03 '13 at 16:18
0

Android activities are stored in the activity stack. Going back to a previous activity could mean two things.

  1. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

  2. Keep track of the activity stack. Whenever you start a new activity with an intent you can specify an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP. You can use this to shuffle between the activities in your application. Haven't used them much though. Have a look at the flags here

AndrewC
  • 32,300
  • 7
  • 79
  • 115
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
0

Have you tried overriding the functionality of the BACK button in Activity D? I'm quite sure this is very related to what you need: Override back button to act like home button

Understand the Android activity life cycle, too.

Community
  • 1
  • 1
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
0
  1. "Pressing back in Activity D should navigate user out of the application"

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                moveTaskToBack(true);           
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    
  2. That's the normal behavior of OS, you don't have to do anything special for it.

ThePCWizard
  • 3,338
  • 2
  • 21
  • 32