Mahfa Mahfa - 10 months ago 80
Android Question

set savedInstanceState Bundle in Activity

is there a way to manually set savedInstanceState Bundle ?

i need to start an activity in savedInstanceState mode that in onCreate the savedInstanceState Bundle won't be null . my point is restoring an activity with my custom data . thanks

Answer Source

This will make your code unreadable and hard to upkeep in the future. Moreover, you will be working against the architecture of the platform.

The correct approach is to send the data in the Intent which starts the Activity, or obtain all data in onCreate if bundle is null, like this:

if(savedInstanceState == null)
    //Get data from db, resources etc.
    //Do the restoring using the bundle, 
    //or just let it pass here and handle it in      
    //`onRestoreInstanceState`, both are correct

Please, don't try to find hack-fixes just because doing thigs the correct way takes a bit more time. It will pay off in the future! :-)

As a side note, please remember, that Intent starting an Activity should not hold big chunks of data, as there is a 1mb cross-process limit for data transferred between components at a time.