Android Navigation Drawer nested activity vs fragment approach - android

first, I'd like to state that I'm new to android development and to gradle as well. not to java.
I had a project supporting 2.3.6 and I'm now in the process of migrating it to 4.4 using the support library.
I'm using Gradle as well, and so far, it's working great.
I'm now using the navigation drawer to navigate between the app's fragments, which used to be activities.
One of the fragments in the drawer holds a listview and a detail view for an item in the list.
The drawer fragment switches it's view when the user clicks an item in the list and shows it's details.
While it's pretty clear to me why it's not recommended for 2 fragments to communicate directly, I still need the list fragment to tell it's details fragment counterpart about the selected item.
The guides specifically say that the activity should implement the callbacks and on the callback methods it should communicate the data between the fragments.
However, in my case, the parent is also a fragment.
My question is should the parent fragment maintain the callbacks?
It seems like the proper solution, since my fragment is only implemented this way to use the navigation drawer swap, and logically, it behaves like an activity, but this would go against the guides saying fragments should not communicate directly.
The question is if implementing the callbacks in the containing fragment is a viable approach or i should change this part of the app to an activity?
Thanks, and sorry for the wall of text.

Your parent fragment should be swapped in via the NavigationDrawerFragment whose callbacks should be implemented in the Activity. The callbacks shouldn't have anything to do with the data passing if I'm understanding you correctly. If the detail is a child of the parent fragment I don't see any reason that the data can't be passed when the child is created.

Related

Should a new fragment started from a fragment in a View Pager be loaded to a new activity?

I would like to create a good base for my app and have it working efficiently. I am a bit lost still with understanding the relationships between fragments and activities (in the basic form I get it, but in cases like the one I need to deal with now I am not sure what to do).
Currently I have 3 fragments in a View Pager, very simple stuff. one of them is for gallery, one of them is for profile and one of them is for forum (imagine the question board here on SO).
Now, inside the forum fragment I have a FAB that should open a new page for asking a new question.
What I am wondering about is, should the new question page be a fragment or an activity? Also, when a question is clicked I would like its thread to open with all the comments. That should be a fragment as well correct?
If fragment, where do I load it into? The View Pager? If so how? Can the view pager host a fragment temporarily? Because the fragment should only be visible when the button has been clicked. Should I place the fragments inside of the forum fragment so it'll serve as it's parent fragment?
I can make it as an activity, but by what I read and know about fragments, it makes sense that it should be a fragment because it is part of the same activity, but when that is that case I am not sure how to do it.
I would actually not use a ViewPager in your case. I use ViewPagers for a series of related screens that deal with a very specific task. For example - a wizard that details instructions where each Fragment might represent a step and it's useful for the user to be able to slide back and forth between instructions.
Given your requirements I would still host everything under one Activity and the individual pages as Fragments inside that activity. Then maybe use something like a bottom nav bar to navigate between the profile, gallery and forum. The screen that is launched by the FAB and another one for viewing the thread of a forum post should be also be separate Fragments as well.
This might be a good opportunity for you to try out the navigation framework under Jetpack. It makes these kinds of things a lot easier to visualize.
See here Android Navigation Framework

Using ViewPager and PagerTabStrip - extending FragmentActivity

First - sorry for the newbish question.
I've started building an app that has a single Activity and a navigation drawer. Each menu item loads a new fragment in the middle frame layout.
I want to create another fragment that:
has tabs
allows for swipe scrolling
It seems like the only way to do this is to create add a ViewPager and PagerTabStrip. All the tutorials I've read indicate the ViewPager requires extending out to FragmentActivity. I have a few questions:
Am I doing anything wrong by replacing the fragment content when navigating menu options?
If what I am doing is ok, is there anyway to incorporate swipe navigation without calling FragmentActivity?
If I need to use FragmentActivity for this one page, I'm assuming I'll call change pages via Intent. Doing so would result in losing the click actions in the navigation drawer. Do I have to call (or duplicate) my code from one activity to another?
EDIT
Sorry about the confusion. Here's what I'd like my application do:
Open app. MainActivity starts. Navigation drawer loads. Main content is loaded via a fragment.
User opens navigation drawer and selects this new menu item I'm creating. It is a new fragment that loads in the frame (like the other menu items). However, it has tabs and supports swiping.
ViewPager is just usual descendant of View so it can't require using of FragmentActivity.
It's absolutely ok.
You don't need to use FragmentActivity. I suppose you just read tutorial about "Implementation of drawer" where author of the tutorial used FragmentActivity.
Can't understand what do you mean. Pages of ViewPager is just views not activities. You don't need to use Intent.
PS Actually I can't understand your problem at all. It's absolutely unclear why you don't want to use FragmentActivity.

Android project code structure for app - deailing with FragmentActivity

I've run into a road block with creating a basic app - due to my understanding of fragments and basic app structure. Could you give me an idea for how my app should be structured?
I have an app with a navigation drawer (currently in the main activity).
The main activity layout has the drawerlayout widget, a frame layout (for fragments), and the list view for the drawer.
Each fragment (or class) is selected thru the navigation drawer.
this has worked fine so far, but I have run into a roadblock. I'd like to create a new fragment which would has tabs. Just about every tutorial I've looked at creates an activity that extends FragmentActivity ... this won't work since FragmentActivity is an Activity.
My question is - is the way I'm structuring my app to work wrong? How should I go about implementing this new tabbed activity / fragment?
Please let me know if you need additional details about the app.
You could use a ViewPager with a FragmentStatePagerAdapter, and add a TabStrip on top. The ViewPager can be contained in a separate Fragment, so would meet your requirement.
See this post for further details Display fragment viewpager within a fragment
You might want to consider launching an activity when an item is selected from the navigation drawer. Just call startActivity(your_intent) in the onClick for the navigation drawer item.
This way the new activity can extend FragmentActivity as the tutorials suggest.

Android Navigation Drawer with many fragments

I've been struggling with this for a while now, I want to start off with a diagram of my problem:
The three navigation drawer buttons are part of my Base Activity. Each purple block below the three buttons are fragments, and the descendants of each of those blocks are in turn fragments. I'll use the master and detail fragments as a demonstration of the issue I am having...The user clicks the nav drawer button, which opens the master fragment that hosts a list of articles. Once a user clicks one of those articles, I then open the detail fragment inside of the master fragment. So If I find myself at the detail, and I decide to open the nav drawer and click the third button for instance, then click the second button again, I want the detail to be open still, and if I hit the phone's back button I want it to move back to the master fragment, and end there. Any tips will be helpful, as I am probably going to use a similar pattern for the first button, it's main fragment and it's children fragments as well.
In my opinion, the cleanest way to handle what you are describing would be to have three separate FragmentActivity classes that implement the DrawerLayout instead of one monolithic BaseActivity.
Each button in the drawer should start it's respective FragmentActivity using launchMode singleTask. This ensures that you launch the same activity instance each time, instead of a new one, which will maintain your back stack for each activity as you switch between them using the drawer buttons. See Android Developer Guide Activity:launchMode for more details.
Each of the three FragmentActivity instances should be responsible for starting and managing it's fragments using listener interfaces. For example, where you have your Master fragment opening a Detail fragment directly, you should instead have your Master fragment tell it's FragmentActivity that it needs to open the Detail fragment. See Android Developer Guide: Communicating with Other Fragments for recommended practices in implementing this type of decoupled communication between FragmentActivity and Fragment. It will make your life much easier down the road when you want different layouts for tablets, etc.
Each of the three main drawer "tasks" seem to be unique enough that isolating each within it's own FragmentActivity seems the best way to implement what you are trying to do. You can apply this same approach for each of your main sections.
I had the same problem and didn't want to go to multiple activities since that would complicate the back navigation of my application. Your fragments won't save their state automatically unless the activity lifecycle events are being called.
In our case those don't happen since we're not leaving the activity. You can use FragmentManager's saveFragmentState on the fragment you are replacing to manually trigger the state saving and get a Fragment.SavedState object. You can keep a list of your SavedState objects and when pushing a fragment check if you have a saved state for it. If so you can call Fragment setInitialSavedState which will cause your fragment to load the previous state.
Now in my app as a user toggles between fragments with their own child fragments the state is retained when they come back.

Implement Tabs with multiple fragments in each tab with seperate back stack

I want to use multiple fragments in each tab of Tab Host.
I am googling for last 5 days but nothing is working in my case. I got a good working solution
Seperate Back Stack and Sample Project Here .This maintain a separate Custom Back Stack for each tab having lots of fragments and store Fragment object in Custom Stack. But when ever I want to re-add any fragment that I already created and stored in its custom stack(as an object), all it's life cycle methods are called once again as are called first time. This is the problem. In this case all the views of fragment layout are recreated and it behaves like a new fragment.
I want to implement functionality like tabs having activity group(in which lots of activities are combined in a single tab using activity group) with the help of fragments.
Please help me in solving this issue........

Categories

Resources