DMCApps / NavigationFragment

The purpose of this manager is to work with the ViewPager, Tabs and Navigation drawer to handle a single stack flow of fragments on the screen. It makes use of a main Fragment as a container and presents and hides fragments within it as children.
MIT License
28 stars 8 forks source link

Behavioral Difference for NavigationFragment 1.0.0 vs 2.0.2-alpha #30

Open jackcsk opened 7 years ago

jackcsk commented 7 years ago

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)?

DMCApps commented 7 years ago

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

jackcsk commented 7 years ago

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

jackcsk commented 7 years ago

@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 commented 7 years ago

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 commented 6 years ago

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