Android app: display overlay elements in AsyncTask

I am creating an Android application that uses google maps to overlay elements. Latitude and longitude for the elements that I get from the MySQL database. To do this, I connect to the php script using HTTP in the Async Task. My code for displaying elements on the map is in the onPostExecute () method of Async Task.

Everything works fine, but when I, for example, turn the phone, all my superimposed elements disappear. How can I solve this problem?

Should overlays of elements occur in the main thread? If so, I need to somehow transfer the information from the asynchronous signals to the main thread, which I studied, but could not get this work. If someone knows a good and correct way to do this, I will be very grateful for the help.

+4
source share
6 answers

OnPostExecute it is called in the main thread! Your problem is when turning the phone

Android restarts the current activity (onDestroy () is called, and then onCreate () is called). The restart behavior is designed to help your application adapt to new configurations by automatically restarting the application with alternative resources that match the new device configuration.

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

+2
source

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

+1
source

The Async task is your thread, in which the onpost method is used by default with the main user interface thread, and not with the Async problem, you just need to process the onConfigurationChanged method, it’s better to post some code.

+1
source

As mentioned, android restarts your activity when you change rotation. So the solution to this in onSaveInstenceState is to store overlay element data, and in onRestoreInstanceState you have to recreate overlay elements based on the stored data.

+1
source

create a method called initUi() and call it in the onConfigurationChanged and onCreate , for example:

 @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) { } 
+1
source

As the other answers say, you can solve your problem using onSaveInstanceState (Bundle outstate) and onRestoreInstanceState (Bundle savedInstanceState) or even turn off orientation changes in the device.

Let's look one by one:

- Changing the orientation of orientation, can lead to the appearance of the user, so I would not approve of it.

-Using onSaveInstanceState (Conclusion abroad) and onRestoreInstanceState (Bundle savedInstanceState) you have two main applications ...

Option 1:

You can save only basic information about the displayed elements (i.e. if it is a map, you can save the location of the center of the map and the scale), and then again get all overlay information from the database. It is very simple, but it can be very slow if you have several hundred overlay elements to get them, which again will lead to a user experience.

Option 2:

You can use disassembly to expand your overlay element so that you can save all overlay elements during onSaveInstanceState (Bundle outstate) and restore them without rebooting from the database. I used this for several thousand items and it works quite well.

Here you can find comprehensive information: Google and an example here: Android - sending data for switching between actions using Parcelable classes

Good luck.

0
source

Source: https://habr.com/ru/post/1436939/


All Articles