Find out when a fragment is permanently destroyed

Is there a way to find out if a fragment is permanently destroyed, i.e. that it will not be recreated in the future?

I need to know this when I must destroy the network task associated with the fragment.

+4
source share
4 answers

Something that should work is to call getActivity().isChangingConfigurations()in the fragment onDestroy().

+2
source

If the task is associated with a fragment, it must be destroyed in the method onDestroy. The system assumes that all fragment resources have been cleared as soon as this method returns, so you just have to go and do the necessary cleansing.

, , , , .

EDIT: , , - (, ), , . :

0

All of this will depend on how you created and customized the fragment (s). An easy way to check if your fragment (s) is permanently destroyed is to add a toast or other type of notification / flag, which you can check in the onDestroy()fragment method . If you see a toast, if the flag is set, you know that the fragment was destroyed.

0
source

In your snippet, override onDestroyand cancel your AsyncTasks or other operations, then

AsyncTask task;

@Override
public void onDestroy() {
    if (task != null && !task.isCancelled()){
        task.cancel(true);
        task = null;
    }

    super.onDestroy();
}
0
source

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


All Articles