10

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

user121190
  • 155
  • 2
  • 11

7 Answers7

19

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).

David Wasser
  • 93,459
  • 16
  • 209
  • 274
4

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();  
            }
        });
    }
}
Masum
  • 4,879
  • 2
  • 23
  • 28
  • 1
    i will not prefer to use **statics**. – Janki Gadhiya May 16 '16 at 07:16
  • Sometimes wired requirement force us to do static @janki gadhiya – Masum May 16 '16 at 07:23
  • This solution is ok, but if we have 15-20 activities then we have to create all activities intance and finish all together. – user121190 May 16 '16 at 07:43
  • Yes. You should do – Masum May 16 '16 at 08:07
  • 2
    This is a horrible solution :-( Android provides `Intent` flags to do exactly what OP wants. Your solution creates memory leaks in the application because the `Activity` instances stored in the static variables can never be garbage collected. Not only that, this solution is difficult to understand and difficult to maintain. There is no reason for such a hack. – David Wasser May 16 '16 at 21:58
2

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.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
1

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.

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
  • There are many views in my Activity N and i don't want to go activity B using intent there are many cases like if i back press i want to go to activity D not B so in this case finish method not works – user121190 May 16 '16 at 07:07
  • thanks for this solution and Flag Intent.FLAG_ACTIVITY_CLEAR_TOP also solved my problem – user121190 May 17 '16 at 04:02
  • complete concept is in this link http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP – user121190 May 17 '16 at 04:29
1

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();
}
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24
  • Using back press i want to go previous activity from which intent is fired so , i want to go activity B on special case that's why i don't want to use finish method. – user121190 May 16 '16 at 07:09
  • 1
    This is a horrible solution :-( Android provides `Intent` flags to do exactly what OP wants. Your solution creates memory leaks in the application because the `Activity` instances stored in the static variables can never be garbage collected. Not only that, this solution is difficult to understand and difficult to maintain. There is no reason for such a hack. – David Wasser May 16 '16 at 21:59
1

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

Drake29a
  • 896
  • 8
  • 23
0

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.