问题
I have made an application where I
- start activity 'Pin' after I start
activity A(activity A hasnoHistory= true, so it will not stay on stack). - If no pin has been set up;
PinActivityalso starts an activityNewPinActivity. - After setting pin,
NewPinActivitycallsfinish()and goes back toPinActivity. Where you will give appropriate pin, it will callfinish()on it and it will move again toactivity A(Sinceactivity AhadnoHistory=true, so I have to start a new instance ofactivity A). My problem is, if I pressbackfromactivity A, sometimes myNewPinActivityshows up again even if I calledfinish()on it.
回答1:
No, you should use in a second activity:
Intent exitIntent = new Intent(this,MainActivity.class);
exitIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(exitIntent);
MainActivity.exitHandler.sendEmptyMessage(0);
and in MainActivity:
exitHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
// clear any informations you like here
MainActivity.this.finish();
break;
}
}
};
回答2:
Sounds like you should be starting NewPinActivity using startActivityForResult. On a successful result PinActivity should launch ActivityA with Intent.FLAG_NEW_INTENT and finish(); itself? Curiously why are you starting your ActivityA with noHistory?
来源:https://stackoverflow.com/questions/25057003/calling-finish-removes-the-activity-from-activity-stack