Closed finneapps closed 7 years ago
@finneapps This is an interesting one. Could you tell me a little bit more about your use case and what you're trying to accomplish? The XML here seems wrong to me. You have two elements that are trying to fill up the entire space of the screen(both have width/height = match_parent ) :
<fragment
android:id="@+id/xml_fragment"
android:name="com.ncapdevi.sample.fragments.XMLFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomBar" />
This would make it so that you couldn't use the FragNavController as the view is being covered by the other fragment that is being displayed on top of the FrameLayout that you've indicated the fragments will be going in. Not sure I understand what you're trying to do.
My code was just an example from your sample app where I quickly wanted to reproduce my nullpointer exception. In my current app that I am working on, I have reused some code from the Android Universal MusicPlayer app: https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/mobile/src/main/java/com/example/android/uamp/ui/BaseActivity.java
If you look at the onStart method, there is a findFragmentById method call. After I added the NavFrag that method returned null: here is the xml: https://github.com/googlesamples/android-UniversalMusicPlayer/blob/master/mobile/src/main/res/layout/activity_player.xml
@finneapps Sorry for the delay on this one, I was on vacation for the last few days. I see now what you're referring to, thanks for the feedback. You are correct, as is the library won't work with XML based fragments, nor is it intended to. It's an interesting concept to explore, but would create some non-trivial challenges (I think, or maybe it's easy, I'd need to spend some more time on it actually).
That being said, is there any specific reason that you absolutely need to imbed the the fragment in the XML? There may be some advantages that I'm unaware of here, but this can all be done without embedding the fragment. Essentially all you'd need is something like:
XMLFragment fragment = new XMLFragment();
mNavController =
new FragNavController(savedInstanceState, getSupportFragmentManager(), R.id.container,fragment);
Where you are creating the fragment on your own and passing it into the controller. Hope this helps!
Hi. I don't need it to be imbedded in the Fragment, but I think it was worth mentioning. I have decided to continue to use your library and just create the Fragment in the Activity onCreate method instead. Thank you for your response.
I'm actually going to mark this as an enhancement because it's worth looking into. Thanks for the feedback.
Hi I just want to let you know that if you add a Fragment inside the XML layout and try to instantiate it after you have created an instance of FragNavController, then the Fragment is null when you do this : XMLFragment fragment = (XMLFragment)getSupportFragmentManager().findFragmentById(R.id.xml_fragment);
but it works as expected when you do this:
My app was crashing because of this