Search code examples
androidandroid-activityrestart

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


I want to use recreate() to relaunch my activity, but I don't want it to execute the onSaveInstanceState(). So, it's really like, launching a new activity.

On this page, an answer says that:

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.

Is there any other way to relaunch an activity within itself without calling onSaveInstanceState()? If you consider this bad practice, what do you think I should do?


Solution

  • have a look here.

    onSavedIstanceState and onRestoreInstanceState are always called, but if you don't implement them they will recreate the app without doing anything :)

    EDIT: you can add a new save to them, like an int.

    when you reload but you don't wanna restore anything, you set this value for example at 1. when you reload and you want to preserve it you set it for example at 0.

    than in onRestoreIstanceState you check for this value, and if it is 1 you don't call any of reload calls, if it is 0 you call them :)