Clear event history stack

I try to clear the event history stack when the user clicks on the logout button , even tried with IntentFlag Intent.FLAG_ACTIVITY_NO_HISTORY , but no luck. Take a look at my script mentioned below.

1) Login screen (end call)

2) The first screen (not causing the finish)

3) second screen (does not end)

4) Third screen (not causing the finish)

5) exit screen (it will open the login screen and end the job)

To achieve my goal m using the code below

Login screen

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,FirstActivity.class); startActivity(intent); finish(); } }); 

login event

 public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (getIntent().getBooleanExtra("EXIT", false)) { this.finish(); }else{ this.finish(); } return true; } return super.onKeyDown(keyCode, event); } 

First screen

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(FirstActivity.this,SecondActivity.class); startActivity(intent); } }); 

Second screen

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(SecondActivity.this,ThirdActivity.class); startActivity(intent); } }); 

Third screen

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(ThirdActivity.this,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("EXIT", true); startActivity(intent); finish(); } }); 

The problem is that when I click the "Exit" button, it goes into the input activity, and when I click the "Back" button, it goes into the second activity.

why it doesn’t complete the action, and also why it goes to the second activity , if it supports the stack, then it should go to the first activity instead of the second one, when I push back from the login screen , I don’t know what kind of thing I am missing.

Update

Everything works fine if I delete finish() when I call the intent from the login screen, but I do not want to stay in the login window on the stack.

Your offer is available.

+4
source share
4 answers

you can do the trick for this question. I used it and worked great with me.

Write the line of code below inside your third onclick link .

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(ThirdActivity.this,FirstActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("GO", false); startActivity(intent); finish(); } }); 

After writing the code below, inside the OnCreate of the FirstActivity class:

  @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // Getting the Value of GO GO = getIntent().getExtras().getBoolean("GO"); if(GO){ setContentView(R.layout.form_data); ... // Here your Code for this Activity ... }else{ Intent intent=new Intent(FirstActivity.this,MainActivity.class); startActivity(intent); finish(); } } 

Also add the Extra value GO value as true when calling the FirstActivity function from MainActivity as below

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,FirstActivity.class); intent.putExtra("GO", true); startActivity(intent); finish(); } }); 

Hope this solves your problem.

+4
source

Change the plane of intent intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); on intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); and try it. I hope this works and helps you.

Clear Top will only work when you perform the operation you are calling in BackStack. Since LoginActivity is not in BackStack, actions are not cleared.

add android:noHistory="true" for LoginActivity in AndroidManifest.xml and try. I give this update after viewing the updated question.

+3
source

Third screen

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(ThirdActivity.this,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_TASK_ON_HOME); intent.putExtra("EXIT", true); startActivity(intent); finish(); } }); 
0
source

Finally, I got one solution! He completed my task, made a static activity object in his own activity, and when he exits the object.finish() system, it works fine for all types of activity.

Below is my trick to achieve my goal

In the first action, declare public static FirstActivity first; and within onCreate by polling first=this; the same as me for other actions, and during the logout, as shown below.

 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ThirdActivity.this, MainActivity.class); FirstActivity.first.finish(); SecondActivity.second.finish(); startActivity(intent); finish(); } }); 
-1
source

All Articles