I have designed an app for android tablets and it consists of a tab bar. I have designed two layouts for each tab as one for landscape and the other for portrait. When the user selects any one of the tab, at first i am showing a progress bar in asynchronous task and some data get loaded.
Currently i am working on with the on restore instance, here when the user rotates the app to either landscape from portrait or viceversa, the asynchronous task is been called again and the data get loaded from the first process.
I tried using the OnConfig method, in this method when rotating from portrait to landscape, the portrait layout is shown in the landscape mode whereas i have designed some changes in landscape layout compared to the portrait one. As i have different layouts for landscape and portrait, i am unable to use the OnConfig.
How to overcome the issue, i dint want to use OnConfig method and i tried Onrestore instance state, both are not solving my issues…pls suggest me a better way……
I do not recommend overriding the
onConfigurationChangedmethod (and not putting orientation as one of your config overrides in the manifest file). With an orientation change, the activity will be recreated, which is great because it will load your new layout with all the correct resource files.As for the AsyncTask, a common pattern would be to not couple this with the Activity. Instead, you could perhaps create a singleton mananger class that holds a reference to the activity as a listener. When the Activity respawns, you can update the manager class with the new Activity instance for the callback and when the async process finishes, it will notify the current activity.
In order to maintain the progress bar, you could keep a state variable that gets saved during
onSaveInstanceStateand restored withonRestoreInstanceStatethat could be used for showing/hiding views. Anther option would be to poll the manager class for the running process and update the layout accordingly duringonCreateThis may not be the quickest and simplest solution but a good framework for this will help down the road.