1

I have 3 Activities, Activity A, B, & C. From ActivityA, I click on a button to launch ActivityB and within ActivityB I click another button to launch ActivityC.

I am trying to figure out the proper way to return to ActivityA from ActivityC with optional return to ActivityB from ActivityC.

If I am in ActivityC and click the home button I want to return to ActivityB, but if I click my 'save' button I want to finish ActivityC & ActivityB and show ActivityA.

How can I accomplish this?

Answer Edit: As krishna murali's suggested in his answer, he was about 99% of the way there. These two flags did the trick Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_SINGLE_TOP

Nick H
  • 8,897
  • 9
  • 41
  • 64
  • Use `startActivityForResult()` and pass the values back to the parent activity during `finsh()` call and decide whether the previous parent should be finished or not based on the button click. [Here is the example](http://stackoverflow.com/a/12293980/4596556) – Madhukar Hebbar Dec 24 '15 at 04:43
  • I think he not aiming at passing back the result. He want to return to ActivityA on the click of button in the activityC.@MadhukarHebbar – Murali krishna Dec 24 '15 at 04:48
  • @krishnamurali He has two cases (Save and Home). One for Activity A and one for Activity B. – Madhukar Hebbar Dec 24 '15 at 04:48
  • I was hoping to do the logic in ActivityC and depending on the situation clear ActivityC and optionally ActivityB depending on the click listener. – Nick H Dec 24 '15 at 04:49

1 Answers1

4

On Click of your button to return to ActivityA from ActivityC. Use this:

Intent intent = new Intent(getApplicationContext(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This will clear all the activities on top of ActivityA.

Murali krishna
  • 823
  • 1
  • 8
  • 23
  • Thanks for the suggestion, this appears to be working. It appears that its starting a new instance of ActivityA, not returning as if I called finish() in ActivityB. – Nick H Dec 24 '15 at 04:48
  • you could use launcherMode = singleTask or singleTop within your manifest – Kosh Dec 24 '15 at 04:52
  • Actually, i added Intent.FLAG_ACTIVITY_SINGLE_TOP as well as CLEAR_TOP and it worked as expected! Thank you everyone. – Nick H Dec 24 '15 at 04:56