Reputation: 10969
I am trying to clear activities history stack, when user click on logout button
even tried with the IntentFlag Intent.FLAG_ACTIVITY_NO_HISTORY
but no luck. Have look at my scenario mentioned below.
1) Login screen (calling finish)
2) First screen (not calling finish)
3) second screen (not calling finish)
4) Third screen (not calling finish)
5) log out screen (It will open login screen, and get finish)
To achieve my goal m using below code,
Login screen
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,FirstActivity.class);
startActivity(intent);
finish();
}
});
login back 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();
}
});
Problem is, when I click log out, it goes to login activity and when I click back button it goes to second activity.
why its not finishing activities and also why its goes to second activity
if it maintaining the stack then it should goes to first activity
instead second one when I click back from login screen
, don't know what sort of thing I missing.
Updated
Everything is working fine if I remove finish()
while calling intent from login screen, but I don't wanna keep in stack login screen.
your suggestion are appriciable.
Upvotes: 4
Views: 12917
Reputation: 10969
Finally I got one solution, anyway! Its fulfilled my goal, make the static object of the activity in its own activity, and while logout object.finish()
for all activities, it working fine.
Below is my trick to achieve my goal
In first activity declare public static FirstActivity first;
and within onCreate
assing first=this;
same thing I did for others activities too and while log out, did like 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();
}
});
Upvotes: -1
Reputation: 5869
Change the intent flat intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
to intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
and try. I hope it works and helps you.
Clear Top will work only when you have Activity you are calling is in the BackStack. As LoginActivity is not in the BackStack, Activities are not cleared.
add android:noHistory="true"
for LoginActivity in AndroidManifest.xml
and try. I am giving this update after seeing your updated question.
Upvotes: 3
Reputation: 25830
you can Do a Trick for this Question. I have Used it and works fine with me.
Write below Line of Code inside your ThirdActivity onclick.
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 Write Below Code inside your OnCreate of 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 put Extra value of GO as true while calling FirstActivity 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 it will Solve your Problem.
Upvotes: 4
Reputation: 10887
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();
}
});
Upvotes: 0