1

I'm now to android. And I'm having trouble in developing application in which I'm supposed to load data from a server and then populate list with this data. I used asyncTask to do so and I used onRetainNonConfigurationInstance() function to handle changing screen orientation while loading the data and it worked well.

The problem started when I wanted to handle changing the locale of my activity. As when I change the orientation the locale changes I've read these:-

How do I save an Android application's state?

and Activity restart on rotation Android

I want to make the layout to change from landscape to portrait So I didn't use

android:configChanges="locale|orientation"

I still can't solve it. I think that the system changes the language on its own so when I change the orientation even if i tried to set it using

Locale locale = new Locale("ar"); 
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

I'll need to finish the activity and open it again which will make the user waits more also it'll make the application crash when the user try to change the orientation again.

I think that I may use onRetainNonConfigurationInstance() but i don't know how with these things I need to handle. So ANY Help will be appreciated.

Community
  • 1
  • 1
FAFI
  • 379
  • 2
  • 6
  • 21

3 Answers3

0

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.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
0

I am just guessing now but maybe you can use

onSaveInstanceState(Bundle)

and

onRestoreInstanceState(Bundle)

to restore your locale after a configuration change.

Torsten Römer
  • 3,834
  • 4
  • 40
  • 53
  • 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 '11 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 changes]http://blog.doityourselfandroid.com/2010/11/14/handling-progress-dialogs-and-screen-orientation-changes/ - this might help you to find a solution. – Torsten Römer Jul 27 '11 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 '11 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 Römer Jul 27 '11 at 14:22
0

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.

Dave
  • 6,064
  • 4
  • 31
  • 38