4

I am building an android application that uses google maps to overlay items. The latitude and longitude for the items I get from a MySQL database. To do this I connect to a php script using HTTP in Async Task. My code for displaying items on the map is in the onPostExecute() method of Async Task.

Everything works fine but when I for example rotate the phone all my overlayed items disappear. How can I resolve this issue?

Should overlaying the items happen in the main thread? If so, I need to somehow pass the information from the async taks to the main thread, which I have looked into but have not been able to get that working. If somebody knows a good and right way to do this, I would really appreciate the help.

pixie
  • 151
  • 2
  • 10

6 Answers6

2

OnPostExecute it is invoked in the main thread! Your problem is that when you rotate your phone

Android restarts the running Activity (onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

.http://developer.android.com/guide/topics/resources/runtime-changes.html

Bianca Daniciuc
  • 920
  • 1
  • 13
  • 22
1

Use the onSaveInstanceState(Bundle outstate) and onRestoreInstanceState(Bundle savedInstanceState) of activity class.

Hardik Nadiyapara
  • 2,436
  • 2
  • 17
  • 24
1

Async task is your thread where onpost method is default attach with main UI thread its not Async problem you just need to handle onConfigurationChanged method, better to post some code.

PiyushMishra
  • 5,743
  • 6
  • 37
  • 57
1

As mentioned android restarts your activity when you change rotation. So solution to this is in onSaveInstenceState you should store you overlay items data and in onRestoreInstanceState you should recreate overlay items based on saved data

Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63
1

create a method named initUi() and call it in onConfigurationChanged and onCreate methods like this:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        try {
            setContentView(...);
            initUi();               
        } catch (Exception e) {

        }
    }

and

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_document);
            initUi();  

        try {
            } catch (Exception e) {

        }
Bob
  • 22,810
  • 38
  • 143
  • 225
0

As said by the other answers you can solve your problem using onSaveInstanceState(Bundle outstate) and onRestoreInstanceState(Bundle savedInstanceState) or even disabling orientation changes in the device.

Lets see one by one:

-Disabling orientation changes, may result in a pour user experience, so I would not favour it.

-Using onSaveInstanceState(Bundle outstate) and onRestoreInstanceState(Bundle savedInstanceState) you have two main oprions ...

Option 1:

You can save only basic information about items being displayed (i.e. if it's a map, you could save map center location and zoom) and then retrieve again from the database all the overlays information. That's very simple but it may bacome very slow if you have several hundred of overlay items to retrieve, resulting again in a pour user experience.

Option 2:

You could use parcelable to extend you overlay Item so you could save all the overlay items during onSaveInstanceState(Bundle outstate), and restore them without reloading from database. I've used this for with several thousand of items, and it works quite well.

You can find parcelable information in here: Google and and example in here: Android – Parcel data to pass between Activities using Parcelable classes

Good luck

Luis
  • 11,978
  • 3
  • 27
  • 35