One way to achieve this, is in activity X, make pressing the back button fire of a custom event.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
backPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void backPressed()
{
Globals.finished = true;
finish();
}
You ll notice the above method has the line Globals.finished = true; This refers to a class called Global with a static (static can be called on a class without having to instantiate an object) boolean variable called finished that is initially set to false as follows. In my own apps I often use a Globals class for sharing things common to the whole app. You can also used the shared preferences to do somthing similar.
public class Globals
{
public static finished = false;
}
Then in the onResume (or possibly onStart) lifecycle method of all activities you can put the following
if (Globals.finished == true)
finish();
这将导致所有活动立即结束,但只有在全球各地的完全变数出现之后。 我以前就这样做了,而且工作得很好,并将确保这些活动把生命周期方法称作结束。
Even simpler than this, you could avoid the use of the back button event handler, by setting the Globals.finished = true, in the onClose method of ActivityX.