How to handle locale and asyncTask for an activity in android when orientation changes?

I am just guessing now but maybe you can use onSaveInstanceState(Bundle) and onRestoreInstanceState(Bundle) to restore your locale after a configuration change.

I tried to do so. But I think that after I set the locale by myself in onRestoreInstanceState(Bundle) I'll finish the activity and restart it again which will make me start loading the data from the server again. Am I right?

– FAFI Jul 27 at 12:40 Maybe I didn't get your question right. So you want to also retain the data you have loaded from the server over config changes? Have a look at Handling progress dialogs and orientation changesblog.

Doityourselfandroid. Com/2010/11/14/… - this might help you to find a solution. – Torsten Jul 27 at 13:16 I used this way to handle changing the orientation and it worked.

But now I have to handle this besides handling changing the locale and I can't figure out a way to use onRetainNonConfigurationInstance() to solve this. How can I use onRetainNonConfigurationInstance() to save the asyncTask and the locale as well? – FAFI Jul 27 at 13:52 Well, then I don't understand why you can't just save your locale in onSaveInstanceState(Bundle) and restore it in onCreate(Bundle)?

– Torsten Jul 27 at 14:22.

Each time configuration is changed, new Resources object is created that points to correct (configuration-dependent) resources. So all your previous changes to Resource object are discarded. In your case you need to re-set locale each time your onCreate() is called.As advoce, do not use android:configChanges unless you really understand all the consequences and have strong reasons to use it.

Please carefully read Handling Runtime Changes for more details. And another tip, it might be a good idea to move your AsyncTask to a dedicated service.As services are better adopted for such "background" tasks and handle configuration changes much better, though more code is required. You have to be very careful when using AsyncTasks that should survive configuration changes.

I've written two posts about background threads in Activites: part 1 and part 2.

If you're using Fragments, consider putting your AsyncTask in a Fragment that retains itself. Add it with a tag, and you can retrieve it again in onCreate. I'm struggling to find it now, but I think this is used in Google's I/O app.

In my AndroidManifest. Note: Using this attribute should be avoided and used only as a last-resort. Please read Handling Runtime Changes for more information about how to properly handle a restart due to a configuration change.

Also, in the 2.3 emulator, it works when switching to landscape, but switching back to portrait fails. Now, the reason why I use configChanges is because when the user switches orientation, I might have an AsyncTask running, doing some network traffic, and I don't want it stopped. Is there any other way of doing this, or is there a way of fixing 2.3 to switch back to portrait?

I know about onRetainNonConfigurationInstance, but I'm not sure it would be a good idea to "save" the AsyncTask instance, mainly because the class that extends AsyncTask is not static (so it is tied to the Activity) -- and it needs to be, because in onPostExecute() it calls methods from the Activity instance.

I am just guessing now but maybe you can use.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions