0

I read a lot about handling rotation in android applications, but I still have so many questions and need to much to understand.

Let me explain my problem or implementation, that I'm using now in my application.

If an activity will be opened, a get request will be sent to server. This request will be executed in a Thread (new Thread(...)) and if request was completed, activity's ui will be refreshed.

But what should I do, if the user rotate his device?

By default, the activity will be destroyed and request will be started again and start a new thread, but the thread of destroyed activity may be still running.

I guess, it's a quite wrong way, I have now.

But what is the best approach, to handle this?

Probably is the best way to forbid rotation, but what If I don't want that?!

May be it's the second part of my question:

I saw a video form Google IO. Mr. Dobjanschi suggested to use services and to store retrieved data in content provider. So, probably I can use a service for executing my requests. But should data be replaced every time the get request was completed?!

Janusz
  • 187,060
  • 113
  • 301
  • 369
Tima
  • 12,765
  • 23
  • 82
  • 125

3 Answers3

0

Well dont know exactly how its done, You can try saving the instance and retrieving the same when config changes with following methods:

I have read about them but haven't really implemented them yet. I hope it can give you some start.

@Override
public Object onRetainNonConfigurationInstance() {
return(myServerThread);
}

private void restoreServerFunctions() {
if (getLastNonConfigurationInstance()!=null) {
myServerThread=(Thread)getLastNonConfigurationInstance();
}
}
Shardul
  • 27,760
  • 6
  • 37
  • 35
  • the thread should be a static, shouldn't it? a static in application or just in activity? ... aren't static bad?! – Tima Nov 21 '10 at 14:45
  • @Mur Votema: Here is an SO question and answer that goes into more detail on this pattern, though using `AsyncTask` rather than a raw thread: http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working – CommonsWare Nov 21 '10 at 14:50
  • thank you for your idea. Mark's answer in another topic describes the full solution – Tima Nov 29 '10 at 09:06
0

You can specify that the activity handles the rotation itself. This is done through adding:

android:configChanges="keyboardHidden|orientation"

in the tag of the activity inside your android manifest. You don't have to actually handle the rotation but this will tell android to not destroy your activity. The base activity class will handle all the rotating of the user interface for you and your thread will be executed correct.

A small side note: if you are doing only a small server task use AsyncTask to execute the call to the server in the background instead of creating a thread. This will minimze some of the programming effort you need to communicate the results from the thread to the activity and update your UI.

Janusz
  • 187,060
  • 113
  • 301
  • 369
-1

One easy way, though I've never tried it. Instead of refreshing the current UI, when the thread finishes, start a new Activity with the just downloaded content. So first, you start an Activity with a blank page (or just the page's frame), then you rotate the blank page as much as you like, then the downloading Thread spawns a new Activity, replacing the blank page Activity with the loaded content page using the current orientation.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • this would also restart the download thread if the user rotates the blank screen. – Janusz Nov 21 '10 at 16:25
  • @Janusz: don't start a new download Thread if the download Thread already starts, you can tell that another downloading Thread already exists by saving a state flag in the Bundle when the current Activity is closed for rotation, and checking it in the onCreate() before starting the download Thread. – Lie Ryan Nov 21 '10 at 16:29
  • @Janusz: the thing that I'm not sure about this method is whether or not Android kills a Thread whose parent Activity is destroyed for rotation. – Lie Ryan Nov 21 '10 at 16:36
  • The thread will not be killed but there is no good way to keep a reference to the thread for the use with the new activity – Janusz Nov 22 '10 at 11:17