Navigation drawer with fragments and only one activity - android

Im doing an app with navigation drawer.
For this, i have a HomeActivity, this contains all the login of my navigation drawer, the options in menu, the view, the titles etc. And here, i set listenerclick for navigation elements.
This listener receives FragmentManager and with a switch do:
smf.beginTransaction().add(R.id.frame_content, new Fragment()).commit();
Replacing fragment for the fragment that i need in each case of switch.
In home layout i have a framelayout and navigation drawer.
Mi question is, is correct that i only have one activity with a framelayout, and depends on the item clicked in navigation drawer i replace the fragment on the frame, or is better have lot of activitys, and create menu in all of them with the same login, and when user click in item menu, launch new intent with the activity selected?
I hope i have explained ok...
Thank you.

I did this same thing, but I found it was a lot better to have different activities.
If you do go down the separate activities path you should have one base activity that the activities extend so you don't need to rewrite the drawer code.
A fragment is only really meant to be an extension of an activity, for example when you have multiple tabs, or you are swiping between different views, or you need to break up your activity into sections.

Related

Create a Navigation drawer to link to fragments and activities

So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.
When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2
but when i click on the Activity 1 the Drawer disappears
but i would like the drawer to continue in the activity as well.
can this be done.
What you'd want to do (roughly) is
Establish a menu as one of your resources and establish the items in that list.
In the activity that you want to contain the drawer, create
the drawer object and create a callback for the onMenuItemSelected.
In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.
This can't be done.
The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity. This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.
However, launching an Activity is different. This is a completely different Activity which has it's own layout.
You actually only have two options if you wish to continue using a Drawer for main navigation.
Remove the need for the second Activity and change that to a Fragment. This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.
Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.

What are the advantages of using Navigation Drawer with Activities VS with Fragments?

I want to implement a Navigation Drawer in my app but I am conflicted on whether I should use it with Fragments or with Activities (see image below for more details).
Is there any real advantages or disadvantages between the two or is it just a matter of preference?
Edit:
Just to clarify my question:
In the case of using Activities instead of Fragments;
When I select "Import" that will open an Activity and not a Fragment and if I select "Gallery" it will open an Activity with contents for gallery item etc. and so on for the other items in the Drawer window.
In the case of using Fragments instead of Activities;
If I choose from any of the Items in the Drawer window it will open their contents in Fragments for each Item selected instead of starting new Activities for each selction.
Remember Fragments need an Activity. You always have one minimum when using Fragments.
If you are talking about to use like main element in the most cases is best use fragments because you have more flexibility UI.
The performance would be better if you have 3 activities and 10 Fragments or have 13 Activities? Think about it, the navigation within the App would be the big challenge but it's just about using the right flow in your application.
Edit:
For instance:
Drawer With Activities instead of Fragments
If you were to use NavigationDrawer without Fragments then it would be best to keep the NavigationDrawer instance in a single Activity and when you navigate the app by choosing from the items in the NavigationDrawer then each of those Activities that are started should not implement the NavigationDrawer but instead should implement the back button to Navigate back to the "Main"/single Activity that the NavigationDrawer was implemented in.
Note: If you do want to implement the NavigationDrawer in multiple Activities you would have to recreate a new instance of the NavigationDrawer in every Activity that you desire to display it.
I suppose this would be a disadvantage vs using Fragments whereas if you used a fragment you wouldn't need many instances of the drawer you would only need one.
Drawer With Fragments instead of Activities
If you use the NavigationDrawer with Fragments then the drawer should be implemented in a single Activity and when each drawer item is selected, their contents are displayed in each of their very own Fragments(which is called inside of the central Activity which manages all the Fragment instances)

Understanding android navigation drawer and fragments

i'm quite new to drawer material and i have trouble understanding some things:
I need to create an Activity with a Fragment on it. Different selections on the Drawer must replace the current Fragment with another one, but is the Drawer something in the fragment or it is one for the activity itself. More specifically is the Drawer living in the Fragment and if not is it possible to create it in the Fragment. I am asking this cause i see that when initiating the Drawer you need to fill parent activity. Also when i tried to use the Navigation Drawer template in Android Studio i didn't had the Use a Fragment checkbox.
There are many ways to achieve what you want. But I think the simplest way is:
1) DrawerLayout view should reside in the activity (probably as the base layout).
2) When you click on an item in the draw 2 things happen:
The fragment is replaced (you have one layout to contain the fragment and you just replace the fragment in it).
The items inside the drawer update (if you are making a list you would simply set the data and call notifyDataSetChanged().
Don't forget to save your state so it can recover in case the Activity is recreated.

Android Drawer Navigation with a ListView, need explanation

I have implemented a basic Navigation Drawer.
I understand Android Drawer Navigation utilizes Fragment (Nav1 opens Frag1, Nav3 opens Frag3).
I want to achieve Frag1 (or the Home Activity) as a list (i.e., ListFragment) And, when an object on the list is clicked it will take me to Frag4....Frag10 depending on what is clicked.
In another term,
Drawer Menu (Nav1...Nav3)
Home Activity (Could be Frag1) is a list and a swipe tab << This is where I am stuck.
Other Activities are Swipe tabs too...
Any suggestion, close example?
Thanks
OK, I have found the answer, a very simple solution. Create ListFragment class. And in the main class just receive the list!
I have taken help from ListFragment

Share ActionBar between different activities

I have an application with an FragmentActivity called MainActivity which holds two fragments, one menu fragment to the left and one content fragment to the right. On a large device both of these fragments are shown. On a small device only the menu fragment is shown and when the user presses an item in said menu the content fragment is displayed in a new Activity.
Lets say the user is currently using the application on his phone and only the menu is visible:
The ActionBar of MainActivity can be manipulated by user choices in the menu. When the user selects a specific menu item, a new Activity launches to display the content fragment. When this Activity pops up the ActionBar of this new Activity is a new ActionBar and therefore does not resemble the ActionBar of FragmentActivity (MainActivity).
I figured I could pass the ActionBar from MainActivity to the Activity which displays the content fragment through Intent extras, or making the ActionBar static and reference it like MainActivity.actionBar or similar, but I ran into problems very quickly :)
Any suggestions on how to solve this? Basically, what I want is to share a single ActionBar reference between all my activities.
Maybe it's better to use fragments instead of activities. I mean show menu fragment, and when you need to show content fragment just put content fragment into the place (container) where was menu fragment. I think better/easier to organize fragment replace/change logics than trying to share actionbar between different activities.

Categories

Resources