ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

Initialisation issue #160

Closed diareuse closed 5 years ago

diareuse commented 6 years ago

Hey,

I have an issue with initialising this library with additional fragment being added from xml. The fragment added from xml will never be accessible to fragment manager, but is indeed attached to activity. Meaning I will never get non-null result from supportFragmentManager.findFragmentById() [or tag()]

In my activity I have:

<somelayout>

  <fragment
    android:id="@+id/overlay_fragment"
    android:name="com.example.OverlayFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /> <!-- This is inaccessible -->

  <FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<somelayout/>

Discovered in 2.4.0, still present in 3.0.0-RC

After removing initialisation (or this library altogether), the fragment remains accessible. Is there a way to handle this with your library or is it just a bug? 🙂

mateherber commented 6 years ago

Maybe it is not documented clearly but you can't have manually inserted fragments in the same container that FragNav uses (the library relies on exclusively controlling fragments in the given container).

diareuse commented 6 years ago

That's absolutely clear, I'm not controlling fragments within @+id/container - that's absolutely logical that it wouldn't work.

But on initialisation he clears all fragments from fragmentManager, which ultimately clears my overlay fragment hence it's inaccessible.

mateherber commented 6 years ago

Ehh then you need a seperate fragmentmanager for it too. I'd suggest to have a "RootFragment" and use its fragmentmanager as the root for FragNav, then it will not mess with the activity's fragmentmanager

diareuse commented 6 years ago

Wait a second, are you suggesting to nest the fragment deeper so it doesn't clear its mates? like so:


<somelayout>

  <fragment
    android:id="@+id/overlay_fragment"
    android:name="com.example.OverlayFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /> <!-- This is inaccessible -->

  <abstractfragment 
    android:id="@+id/containerRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"><!-- separate fragment -->

    <container for FragNav here>

  </abstractfragment>

<somelayout/>
mateherber commented 6 years ago

Yes I'm basically suggesting that. I'm not sure if that works out. It seems fragnav relies on being exclusive on the fragmentmanager it works on (not just a container). Maybe this behaviour could be changed (I'm not even sure what I suggested above works, worth a try though)

diareuse commented 6 years ago

I will try that, thanks for suggestion. However it still seems more like a workaround than a fix to me though. It is indeed trying to be exclusive with the parent's fragmentManager