1

I have an application that goes as follows (Home being the launch activity):

D
C
B
A
Home

My flow goes as follows:

The user starts activity A from Home, which flows to B and C. When the user leaves activity C, I want A, B, and C to be destroyed. That is, if the user presses BACK in activity D, it goes back to Home.

The user must be able to control program flow normally through activityA, B, and C. So if they press the back button in activityC, it goes back to activity B.

I've looked at Intent flags such as CLEAR_TOP and NEW_TASK, but none of them seem to do what I want them to.

I'd appreciate any help!

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
Codeman
  • 12,157
  • 10
  • 53
  • 91

2 Answers2

4

Perhaps you are looking for FLAG_ACTIVITY_TASK_ON_HOME? It requires API level 11 though :(

for API level <11, this can be done:

when starting activity B and C, use startActivityForResult(). When starting activity D, do this:

startActivity(D);
setResult(KILL_YOURSELF); //KILL_YOURSELF is some arbitrary int that you use to identify that the other activities should exit
finish(); //finish the activity

This will kill activity C. Then in activity A and B, override onActivityResult like this:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == KILL_YOURSELF) {
            setResult(KILL_YOURSELF);
            finish();
        }
    }

Thus activity B will finish, which in turn will trigger onActivityResult in A, so it will also finish.

pgsandstrom
  • 14,361
  • 13
  • 70
  • 104
  • I'm programming for API 7+, unfortunately. Good try, though! – Codeman Aug 16 '11 at 18:06
  • Well, since that flag seem to do what you are looking for, I would conclude that you cant get what you want with a flag. You can do it manually though, by some somewhat ugly code. I.e. finish activity A,B and C with finish() as you launch D :) – pgsandstrom Aug 16 '11 at 18:08
  • Yes, but how do I access A,B,C from D other than creating a static array (which would be horrifying memory-wise)? – Codeman Aug 16 '11 at 18:11
  • When launching from C, finish C. Make sure that C was started with startActivityForResult(), so that B:s method onActivityResult() is triggered. Thus you can finish B, and then A in the same fashion :) This is a tad ugly, but I see no better option. – pgsandstrom Aug 16 '11 at 18:15
  • I thought of a recursive call... could you provide a lightweight example? I haven't used onActivityResult() for anything but retrieving contacts and logging users in. – Codeman Aug 16 '11 at 18:18
  • I edited my answer, try it and see if it works out for you :) – pgsandstrom Aug 16 '11 at 18:39
  • I'm using MVC methodology, so I don't have direct access to overrides (not sure why, I'm working on a codebase someone before me wrote...), but this looks correct, and I'll give it a try after some refactoring. Marking it as correct. Thanks! – Codeman Aug 16 '11 at 18:47
  • 1
    Love the constant name KILL_YOURSELF - it got a chuckle. – Travis Dec 21 '11 at 13:31
0

Simply intercept the Back Button in Activity D, and upon intercepting the Back Button, head over to the Home Activity. You may / maynot want to finish 'D' activity as you are heading over to the Home Activity.

Community
  • 1
  • 1
mastDrinkNimbuPani
  • 1,249
  • 11
  • 14