NavigationFragment icon indicating copy to clipboard operation
NavigationFragment copied to clipboard

Behavioral Difference for NavigationFragment 1.0.0 vs 2.0.2-alpha

Open jackcsk opened this issue 7 years ago • 5 comments

I've spotted lifecycle behavioral differences for NavigationFragment 1.0.0 vs 2.0.2-alpha

On Fragments inherited NavigationFragment with 1.0.0 version, presenting another fragment will result in first fragment calling onPause() and onStop() Whereas on 2.0.2-alpha, the onPause() method is not called. In addition, content for previous fragment is not cleared out.

My question is: Is this expected, and how can I get the behavior of v1.0.0? (i.e. NavigationFragment calling onPause() and onStop() methods on presenting another NavigationFragment)?

jackcsk avatar Sep 07 '17 09:09 jackcsk

Can you give me the snippet of code you are using to transition from one NavigationFragment to another?

DMCApps avatar Sep 07 '17 21:09 DMCApps

@DMCApps I've setup a demonstration project to show the differences between the 2 versions: https://github.com/jackcsk/NavigationFragmentDemo

jackcsk avatar Sep 08 '17 04:09 jackcsk

@DMCApps I've added the following to SampleFragment.java:

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart");
    }

    @Override
    public void onStop() {
        Log.d(TAG, "onStop");
        super.onStop();
    }

and it shows the same behavioral change for 2.0 version as well - onStart and onStop behave differently than 1.0 version of NavigationFragment. I am still looking into why that happen (since my fragment implementation depends on calling those 2 methods)

jackcsk avatar Oct 03 '17 04:10 jackcsk

After comparing 2.0 release with 1.0.0: If I add the code snippet from the patch file into navigationfragment/common/core/StackManager.java, the onStart() and onStop() methods works just like 1.0.0 release. patch.diff.zip

diff --git a/navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/core/StackManager.java b/navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/core/StackManager.java
index 61ac65a..502af2c 100644
--- a/navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/core/StackManager.java
+++ b/navigation-fragment/src/main/java/com/github/dmcapps/navigationfragment/common/core/StackManager.java
@@ -30,6 +30,15 @@ public class StackManager implements Stack {
             transaction.setCustomAnimations(0, 0, 0, 0);
         }
 
+        State state = navigationManager.getState();
+        if (state.getStack().size() >= config.getMinStackSize()) {
+            Object childManagerFragment = navigationManager.getContainer().getNavChildFragmentManager();
+            FragmentManagerWrapper fragmentManagerWrapper = new FragmentManagerWrapper(childManagerFragment);
+
+            Navigation topFragment = (Navigation)fragmentManagerWrapper.findFragmentByTag(state.getStack().peek());
+            transaction.detach(topFragment);
+        }
+
         // Add in the new fragment that we are presenting and add it's navigation tag to the stack.
         transaction.add(config.getPushContainerId(), navFragment, navFragment.getNavTag());
         transaction.addToBackStack(navFragment.getNavTag());
@@ -55,6 +64,16 @@ public class StackManager implements Stack {
 
             if (state.getStack().size() > 0) {
                 navFragment = (Navigation) fragmentManagerWrapper.findFragmentByTag(state.getStack().peek());
+                Object transaction = fragmentManagerWrapper.beginTransaction();
+                if (transaction instanceof android.support.v4.app.FragmentTransaction) {
+                    ((android.support.v4.app.FragmentTransaction) transaction)
+                            .attach((android.support.v4.app.Fragment) navFragment)
+                            .commit();
+                } else if (transaction instanceof android.app.FragmentTransaction) {
+                    ((android.app.FragmentTransaction) transaction)
+                            .attach((android.app.Fragment) navFragment)
+                            .commit();
+                }
             }
         }
         else if (navigationManager.getContainer() != null){

Just wonder any reason for the removal of the code snippet in 2.0?

jackcsk avatar Oct 03 '17 08:10 jackcsk

bumped, just wanna check if the above change to StackManager class is ok. I can submit a pull request if necessary.

jackcsk avatar Nov 15 '17 02:11 jackcsk