开发者

Handling of server requests and device rotations

开发者 https://www.devze.com 2023-01-26 18:08 出处:网络
I read a lot about handling rotation in android applications, but I still have so many questions and need to much to understand.

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 no开发者_如何学编程w.

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?!


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();
}
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消