5

I have 4 activity let suppose activity "A","B","C" and "D" when I move from C>D>A and when I press Device back button from Activity "A",then "C" became visible to me instead of exist from app.How can I acheive this.

Raghav
  • 137
  • 5
  • 11
  • Does this answer your question? [Android remove Activity from back stack](https://stackoverflow.com/questions/14112219/android-remove-activity-from-back-stack) – Sir Codesalot Jan 19 '23 at 13:15

5 Answers5

5

You can destroy your activity when you call the next activity. So after Intent you can call finish() method to destroy that activity.

Check this one

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();

When you use this it does not go to previous activity instead it finishes the activity.

OR
You can also use NO_HISTORY flag

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
1

Override your backpress like this, from where you want to go back to home screen

@Override
    public void onBackPressed() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle("Exit");
        builder.setMessage("Do you want to exit?");
        builder.setInverseBackgroundForced(true);
        builder.setNegativeButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    /*finish();
                    System.exit(0);*/
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            });
        builder.setPositiveButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
        builder.create();
        builder.show();


    }
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
0

I guess you are trying to exit from your app. Here is your answer: How to exit from the application and show the home screen?

@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
Community
  • 1
  • 1
Efe Budak
  • 659
  • 5
  • 27
0

In Activity A you can finish all activities in onResume for that you need to store all activity's this in staic variable

Activity B

public static B b;
oncreate(){
//...
b=this;
}

Do the same for C and D and in A

onResume(){
   clearAllTasks();
}

public void clearAllTasks(){
 if(B.b!=null){
   B.b.finish();
 }
 if(C.c!=null){
   C.c.finish();
 }
 if(D.d!=null){
   D.d.finish();
 }
}

in manifest

<application>
  <activity android:name="A"
     android:noHistory="true">
  </activity>
  //...
</application>
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
0

Working for me:

   restart.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                                Intent.FLAG_ACTIVITY_NEW_TASK |
                        Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(restart);
Javier Monzon
  • 59
  • 1
  • 1