Avoid asynchronous update fragment when changing screen orientation

I am developing an application for an Android tablet, and I need to implement the screen orientation (landscape and portrait). when the orientation of the screen changes the reload of the asintact fragment. (sometimes application crashes) I need to stop restarting the asynthesis and request the data again. want to load existing json data to a new screen. any help appreciated.

approch: android:configChanges="keyboardHidden|orientation|screenSize"does not work due to im using the fragment class.

+4
source share
3 answers

( ), - , AsyncTask are evil .

AsyncTask , Fragment, AsyncTaskLoader. javadoc , .

, (.. LoaderManager.LoaderCallbacks) , (, , ), , , .

, , Loader , , , .

, , , ( /), , .

+1

, . AsyncTask onCreate(), . , , onCreate() , .

+1

" " , setRetainInstance .

public void setRetainInstance (boolean )

, Activity (, ). , . , :
• onDestroy() ( onDetach() , ).
• onCreate (Bundle) , .
• onAttach (Activity) onActivityCreated (Bundle) .

, , onCreateView, :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    setRetainInstance(true);
    // ... more of your code
}

, , , .

, setRetainInstance onCreateView ( ), /, , , onCreate ( ), " " .

, .

+1

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


All Articles