我会质疑你为什么要这样做......但这是我想到的第一件事:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Log.v("Example", "onCreate");
getIntent().setAction("Already created");
}
@Override
protected void onResume() {
Log.v("Example", "onResume");
String action = getIntent().getAction();
// Prevent endless loop by adding a unique action, don't restart if action is present
if(action == null || !action.equals("Already created")) {
Log.v("Example", "Force restart");
Intent intent = new Intent(this, Example.class);
startActivity(intent);
finish();
}
// Remove the unique action so the next time onResume is called it will restart
else
getIntent().setAction(null);
super.onResume();
}
您应该"Already created"
设置唯一,以免其他 Intent 意外执行此操作。