OnDestroyView for a fragment never called after onStop

My APP has a few snippets and actions. Most of these actions contain different fragments. This is to ensure that my components are easily reused. I am having problems loading other actions onto the activity stack.

Case
Launched ActivityA β†’ ActivityB β†’ ActivityC

All these actions contain different fragments, but the problem is that when starting ActivityBfrom ActivityAfragments, the fragments in ActivityA onDestroyVieware not called, although onStop receives the call.

My APP allows an infinite amount of navigation from one to the other, when I add too much activity to the application stack, it gradually throws an OOM exception.

Find the code below that I use to add a fragment to the fragment stack.

final android.support.v4.app.FragmentTransaction ft =
                fragmentManager.beginTransaction();
if(transaction.mInAnimation != FragmentTransaction.FRAGMENT_NO_ANIMATION &&
                transaction.mOutAnimation != FragmentTransaction.FRAGMENT_NO_ANIMATION) { 
    ft.setCustomAnimations(transaction.mInAnimation, transaction.mOutAnimation);
}
String tag;
if(transaction.isRoot){
   clearFragmentStack();
   tag = "0";
}else {
   tag = fragmentManager.getBackStackEntryCount() + "";
}
final AtomicFragment fragment = transaction.compile();
ft.replace(transaction.mFrameId, fragment,  tag);
ft.addToBackStack(tag);
ft.commit();
+3
source share
4 answers

So, your problem is that β€œwhen you add too much activity on the stack, the application gradually throws an OOM exception”, and you think that the reason may be that it is onDestroyView()not called in the upper fragment when switching.

OnDestroyView ()

Firstly, when you switch from Activity1 to Activity2, most likely onDestroyView()it is not called in your fragment in Activity1, because you did not call finish()in Activity1 after starting Activity2.

, Activity1 backstack, (.. onStop ). Activity1 , . backstack Activity1 . , Activity1 , , "".

onDestroyView() , . backstack/activity.

, (.. fragmentManager.popbackstack() beginTrasaction.remove(...))), Activity1 Activity2 (.. finish()) - onDestroyView() Activity1.

OutOfMemoryException

... , OOM .

, OOM , Activity , , onDestroyView() . , .

, android:launchMode="singleTask" () AndroidManifest.xml. , . OutOfMemory, Activity.

, reset / "". , onNewIntent(...) (reference), , .

:

OutOfMemory: Android Memory Monitor . , - , () .

, - , , (Activity1, ).

, - : enter image description here

, ( ), . , , , .

,

+5

, , ,

onDestroy()

onDestroyView(), onDestroy()

+1

, , . , , . :

<activity android:name=".MainActivity"
        android:launchMode="singleTop"/>
0

@Override
public void onDestroy() {
    super.onDestroy();
}
@Override
public void onPause() {
    super.onPause();
}
@Override
public void onResume() {
    super.onResume();
}
@Override
public void onStop() {
    super.onStop();
}
-2

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


All Articles