如何在不保存实例的情况下在自身内部重新创建 activity?

How do you recreate an activity within itself without saving its instance?

我想使用 recreate() 重新启动我的 activity,但我不希望它执行 onSaveInstanceState()。所以,这真的很像,推出一个新的 activity.

this 页面上,一个答案是这样的:

Since API level 11 (Honeycomb), you can call the recreate() method of the activity (thanks to this answer).

The recreate() method acts just like a configuration change, so your onSaveInstanceState() and onRestoreInstanceState() methods are also called, if applicable.

有没有其他方法可以在不调用 onSaveInstanceState() 的情况下在其内部重新启动 activity? 如果您考虑到这种不良做法,您认为我应该怎么做?

这可能不是好方法,但我处理的方法是重用启动 activity 的意图。 在 onCreate 方法中定义一个意图 intentOLD 并使用 intentOLD = getIntent() 检索启动此 activity 的 Intent。然后当你想重新启动 activity 时,调用 finish();开始活动(intentOLD);

看看here.

onSavedIstanceStateonRestoreInstanceState 总是被调用,但如果你不实现它们,它们将重新创建应用程序而不做任何事情:)

编辑:你可以给它们添加一个新的保存,就像一个 int。

当您重新加载但不想恢复任何内容时,您可以将此值设置为例如 1。 当你重新加载并且你想保留它时,你将它设置为例如 0.

比在 onRestoreIstanceState 你检查这个值,如果它是 1 你不调用任何重新加载调用,如果它是 0 你调用它们:)