Android-Droid
Android-Droid

Reputation: 14565

Android finish current activity and start parent activity

I have a little question which is bothering me. How can I finish activity C and start it's parent. But the tricky part is that I can start activity C from 20 other activites. The idea is to open the right one when i call finish on C. And the other thing is that I have tabhost , which childs opens activity C.

Any suggestions how to achieve this?

Upvotes: 2

Views: 10636

Answers (4)

Android-Droid
Android-Droid

Reputation: 14565

I simply did something like this :

 Intent intent = new Intent();
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 finish();

This did the trick.

Upvotes: 2

Bogie
Bogie

Reputation: 153

Take a look at Task and Backstack, Implementing Temporal and Ancestral Navigation

There are more specific explanations. Hope this help

Upvotes: 0

STT LCU
STT LCU

Reputation: 4330

in your activity C save the following variable:

Class parent = ParentActivityClass.class;

override:

public void onBackPressed(){
    //create an intent like
    Intent i = new Intent(this, parent);
    startActivity(i);
    //add extras to intent if needed
    this.finish();
}

please note that this might create a NEW parent activity. it is up to you handle this situation if this might create problems.

An alternate solution is to finish each other child activity when you launch a new activity. This will assure that on your stack you will have always the parent below the child activity.

Upvotes: 2

ud_an
ud_an

Reputation: 4921

Depend on your activity stack if your current exactly on top of the parent activity you can just finish current actvity and it will go to previous activity. If you want to clear all activity stack and start over new activity try

 Intent intent1 = new Intent(context, activity.class);
             intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
            startActivity(intent1);

which clear your stack and start over new if you have stack of activity over parent want to finish all use this and start over parent again.

Upvotes: 1

Related Questions