user121190
user121190

Reputation: 165

How to finish specific activities not all activities

In my android application suppose several activities are there if using intent I go to other activities like this

[Activity A]->[activity B]->[Activity C]->[Activity-D]->[Activity N]

and now when am on activity N when I pressed button then I want to go to Activity B and want to destroy Activity C And Activity D but Activity A should not destroy. I also searched in various posts but I didn't get exactly the same solution. Any help will be appriciated

Upvotes: 11

Views: 8347

Answers (7)

You can start ActivityC, ActivityD, ActivityN with the same request code passed to startForResult(requestCode)

And then at ActivityN, use finishActivity(int requestCode).

Documentation for finishActivity(int requestCode)

Force finish another activity that you had previously started with startActivityForResult.
Params:
requestCode – The request code of the activity that you had given to startActivityForResult().
If there are multiple activities started with this request code, they will all be finished.

Upvotes: 0

David Wasser
David Wasser

Reputation: 95578

In ActivityN, to return to ActivityB use this:

Intent intent = new Intent(this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

Using the flag CLEAR_TOP will finish all activities in the stack that are on top of ActivityB. In the example you gave, this will finish ActivityN, ActivityD and ActivityC. Using the flag SINGLE_TOP will make sure that this will return control to the existing instance of ActivityB (ie: it won't create a new instance of ActivityB).

Upvotes: 21

Drake29a
Drake29a

Reputation: 896

Flag Intent.FLAG_ACTIVITY_CLEAR_TOP may solve your problem: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Upvotes: 1

Janki Gadhiya
Janki Gadhiya

Reputation: 4510

EDIT : Use startActivityForResult() instead of startActivity()

So depending on the result you can change the behavour.

Say for example When you wanted to go to ActivityB just return some flag in the INTENT. When it will be caught in Activity D and C in onActivityResult(), finish them and you will be finally on B.

Upvotes: 1

Masum
Masum

Reputation: 4959

In Your Activity C do like this

public static ActivityC instance = null;
public class ActivityC extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
    }
}

And in your Activity D do like this

public static ActivityD instance = null;
public class ActivityD extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
    }
}

Finally in your Activity N. Do Something like this

public class ActivityN extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button yourButton= (Button) findViewById(R.id.yourButton);
         yourButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityC.instance.finish();
                Activityd.instance.finish();
                finish();  
            }
        });
    }
}

Upvotes: 4

Ruchira Randana
Ruchira Randana

Reputation: 4179

Here's my approach.

From Activity A, don't just start the Activity B, call startActivityForResult() method. Do this for all subsequent calls.

Now, when you press the button from Activity N, set the result for a custom value and call the finish() method for Activity N. Now you should hit the onActivityResult method on your Activity D. Now you can check whether the result was you pressing the button. Depending on your result, keep on setting the result and subsequently calling finish() on each Activity.

This should technically work.

Upvotes: 2

Damini Mehra
Damini Mehra

Reputation: 3347

Try this code: //Activity A

Intent i = new Intent(getApplicationContext,ActvityB.class);
startActivity(i);

//Activity B

Intent i = new Intent(getApplicationContext,ActvityC.class);
startActivity(i);

//Activity C

Intent i = new Intent(getApplicationContext,ActvityC.class);
startActivity(i);
finish();  
// finish here actvity which you want to finish

//Try this second way:

In your first activity, declare one Activity object like this,

public static Activity fa;
onCreate()
{
 fa = this;
}

now use that object in another Activity to finish first-activity like this,

onCreate()
{
 FirstActivity.fa.finish();
}

Upvotes: 1

Related Questions