Asynchronous task inside fragments during screen processing

How to save an asynchronous task during screen loading. I searched a lot, but did not find anything elegant for him.

What are my problems:

  • I have different layouts for portraiture and landscape, so you can’t use android:configChanges="keyboardHidden|orientation|screenSize"outdoor activities to limit it.

  • I can’t use Fragment.setRetainInstance(boolean), because I have nested fragments, so I add fragments to the back stack usingaddToBackStack()

    In this situation:

    onCreate()and are onDestroy()not called. This is confirmed by the API docs.

    The onCreateView()packet sent from is not received onSaveInstanceState(). The transmitted packet is empty. Therefore, I will not receive the saved instanceactivity

How to save an asynchronous task in fragments to avoid failures when changing orientation.

Or is there any other way to perform an asynchronous task.

+4
source share
2 answers

If you cannot use the fragment save instance, you must use the Service or IntentService to start your task. The idea of ​​the Service is that it continues to process whatever happens in the Activity. Since you did not say something about the work of the Task, decide which one is suitable for your application.

AsyncTask Activity, - , . , , Fragment. , onSaveInstanceState(), . : .

, , async ( ) , , .

0

AsyncTask (. Intent.FLAG_ACTIVITY_NEW_TASK), Activity, .

Handler / .

0

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


All Articles