3

I have 3 activities A, B and C. A leads to B which leads to C. I would like to be able to move back and forth between A and B but I want to finish both A and B once C gets started. I understand how to close B when starting C via the intent but how do I also close A when C gets started?

duncanportelli
  • 3,161
  • 8
  • 38
  • 59
  • http://stackoverflow.com/questions/12947916/android-remove-all-the-previous-activities-from-the-back-stack –  Apr 03 '13 at 16:19
  • 2 weeks ago you asked [exactly the same question with exactly the same text](http://stackoverflow.com/questions/15556138/finishing-activity-from-another-activity) using another user name. That question was closed as a duplicate. If you search a bit on StackOverflow you could find dozens of valid answers to your question. – David Wasser Apr 03 '13 at 18:14
  • That was not me. I saw that question and tried all the answers but I didn't succeed. That is why I posted the same question again – duncanportelli Apr 03 '13 at 18:22

4 Answers4

15

In your onCreate() method assign a static instance to a variable to create a Singleton:

public static ActivityA instance = null;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
}

@Override
public void finish() {
    super.finish();
    instance = null;
}

then in C:

public void onCreate(Bundle savedInstanceState) {
    if(ActivityA.instance != null) {
        try {  
            ActivityA.instance.finish(); 
        } catch (Exception e) {}
    }
}

(Repeat above code for B as well as A)

I would say this is NOT elegant and is bound to introduce strange lifecycle bugs.

It would be better if you could tweak your requirement - if you can't you could perhaps use a single Activity and change A, B and C into Fragments?

I would have suggested a Broadcast but as I understand it you can't have instance level broadcast receivers on "non resumed" Activities.

Using a Service to bind to the Activities seems like overkill - but you might want to consider that if the above doesn't work.

Good luck!

Graeme
  • 25,714
  • 24
  • 124
  • 186
3
((Activity)context_of_another_activity).finish();
Taryn
  • 242,637
  • 56
  • 362
  • 405
1

I realize this is a very late response, but I feel the BroadcastReceiver is the best approach to this question.

In Activity A add this

    //class object
    BroadcastReceiver receiver;

    //add this to onCreate()
    receiver =new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };
    IntentFilter filter =new IntentFilter();
    filter.addAction("FINISH_A");
    registerReceiver(receiver, filter);

   //add this to Activity A as well
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

Then, in Activity C onCreate() (or when ever you want to finish Activity A)

Intent local =new Intent();
    local.setAction("FINISH_A");
    sendBroadcast(local);
seekingStillness
  • 4,833
  • 5
  • 38
  • 68
-1

When you start C from B, do it in this way:

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Now, C starts, but A and B are gone.

However, I think it is better if you can rethink your design such that C does not depend on A being finished or not.

Nazar Merza
  • 3,365
  • 1
  • 19
  • 19
  • 2
    No, using your code `C` would not start. `B` would be finished and `A` would also be finished and then another instance of `A` would be created (based on using `Intent.FLAG_ACTIVITY_CLEAR_TOP`) – David Wasser Apr 03 '13 at 18:07