I have a navigation drawer with few contents. On click selected navigation content i'm adding ListFragment into activity container. When i clicked on the selected ListFragment position i want to show detail of this selected content. What is the better approach:
Replace the MainActivity container(which is ListFragment) by DetailFragment.
New DetailActivity which include the DetailFragment.
Or there is a other approach.
So you essentially need to have a ChildFragment inside your ListFragment
Use getChildFragmentManager() inside your ListFragment for your DetailFragment transactions. This is the best approach if you need to notify anything to your ListFragment from your DetailFragment in future.
So your activity will just have the fragment holder for your ListFragment. Your ListFragment will have the fragment holder for your child fragments, in your case, DetailFragment.
Having a separate DetailActivity for each list view click is good but not the best approach as you will lose your ListActivity context then.
For reference : https://stackoverflow.com/a/17132254/1115353
I believe in the most of cases it's better to replace old fragment with a new one when you are switching between drawer menu items. If you go deeper in already existing fragment on MainActivity you should use "add" method with saving fragments in stack.
If you want to change content from Drawer - it's better to replace fragment.
If you want to add new content above existing, like adding details, you should use "add" method
The second approach is the better, where the Gmail app uses that approach.
Related
I have an container activity which will contain the fragments which will be added to this container when my bottom nav bar is navigating. Now I want to go to one of this fragment to another fragment which will replace another different activity by set on click listener of a cardview.
I am new fragment so I dont understand how to do that. Hope someone will help me.
You approach is wrong. You cannot navigate from one activity fragment to another activity fragment. The correct way will be to have single activity with multiple fragments inside. Using Navigation component will be the best way of navigation.
I have a ListFragment that has a recyclerview and onClick on any items opens the DetailsFragment. The details fragment contains another recyclerview on the bottom that shows "MORE ITEMS". Now onClick on any of these items should open the DetailsFragment for that particular item. Basically the fragment needs to be refreshed.
In the past, I would just replace fragment using fragmentManager. How do I go about refreshing the fragment? How can I create an action that points to the same fragment?
Creating an action from DetailsFragment to DetailsFragment and navigating to this action worked as mentioned by Alex
The best way to update a fragment is to use a shared viewmodel between fragments or activity/fragment.
You can read the documentation here https://developer.android.com/topic/libraries/architecture/viewmodel#sharing
Activity or Fragment A is doing some operation, at the end of it updates the viewmodel. Fragment B (that needs to be updated) is observing the same viewmodel and can update the values in the UX.
Creating an action that reloads the fragment is time consuming and not always suitable for each use case.
I've created an app, that has a main activity with a drawer menu so the user can click on some option and a fragment is open inside the activity.
What I'd like to do is to have several screens in one of the options and to navigate between them by tabs/slide the screen.
I saw an example of doing it inside one activity and with a fragment per 'sub-screen', my question is: how can I do it when I'm already 'inside' a fragment?
Thanks
Fragments can support having other Fragments inside them. The key to making this work is to remember to use getChildFragmentManager() to get the Fragment's FragmentManager instead of getFragmentManager() which gets the Activity's FragmentManager.
If you want to swipe between views, use a ViewPager inside your Fragment UI. The ViewPager will use a FragmentPagerAdapter to handle the Fragments for display.
In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.
I'm having a bit of trouble with communication between a parentActivity and its child fragment.
I am using actionbarsherlock.
I have looked around on SO, but I haven't found something that deals with ViewPagers and Spinners yet.
Background:
My child fragment has its own action bar specifying a couple of extra action items not available in other fragments, one of these action items is a spinner with multiple options - A, B and C etc
By default the MainActivity will load Fragment_A(child fragment) which contains a ViewPager.
This view pager handles swipes between views A and B(Spinner has options A,B, C etc)
Fragment_A also has spinner adapter. If you select A on the spinner, the viewPager switches to A, if you select B on the spinner, the viewPager switches to B. (This works fine).
My question is: How do I capture when the user "swipes" between A and B(They don't use the spinner) and change my "spinner" to say A or B respectively?
Note: My pager adapter(for ViewPager) is created separately in a different class.
Any help is appreciated...Thank you all!
This is what you need. You'll have to set up an interface in your Fragment and implement that interface in the parent activity. In your Fragment instantiate an instance of the interface and in whatever method you use to listen to page changes, call the method to communicate that data to the activity.
It seems kinda "hand-wavy" but I hope that clears it up a little.