Android finish current activity and start parent activity

10,593

Solution 1

I simply did something like this :

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

This did the trick.

Solution 2

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.

Solution 3

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.

Share:
10,593
Android-Droid
Author by

Android-Droid

Android developer.

Updated on June 14, 2022

Comments

  • Android-Droid
    Android-Droid about 2 years

    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?

  • Android-Droid
    Android-Droid over 12 years
    i want to return to the previous activity and I forget to mention that I want to clear the history, but what should i put instead of activity.class ?
  • ud_an
    ud_an over 12 years
    the activityname you want to redirect as a class