Change Navigation Drawer selected item from within Fragment - android

I have a navigation drawer in my main activity, and 2 fragments (A and B). The Navigation has two items (A and B). When the application is started the main activity shows and displays fragment A in the FrameLayout.
Fragment A contains a button that replaces the FrameLayout content to Fragment B. The .replace works fine, but I'm not sure how to change the selected drawer item from A to B.
Jayesh helped me with this... didn't realize it was easy. For those interested in my main activity (which hosts the navigation drawer):
public static void changeDrawerItem(int Position) {
mDrawerList.setItemChecked(1, true);
}
Note: mDrawerList is my Drawer List. Not sure if this is the best way to change the item, but it'll allow me to change the drawer item from other fragments I create.
In the fragment I simply call the method with
MainActivity.changeDrawerItem(1);

Related

How to set toolbar.setNavigationOnClickListener only for current fragment?

Started new project from navigation drawer activity template. By default, all drawer menu items are at the same navigation level. All fragments display hamburger menu button, that shows drawer.
I need to keep all items in drawer, but place Home as top level item and others as it's children like this:
Tried to overwrite toolbar?.setNavigationOnClickListener { } in Gallery fragment, but it also affects Home fragment and I didn't find how to restore default behavior.
How can I set Home as navigation parent for others or how to set navigation click listener to only one fragment?
For example you have set tag for every fragment to trace when you start new fragment and now check with Home and set click event..
Fragment home = getSupportFragmentManager().findFragmentByTag("home");

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.

android open a fragment which is in navigation drawer from an activity which is not navigation drawer

I have 4 buttons on the main activity screen.
I also have a menu with 4 items and I want to show it using a navigation drawer. I described these 4 items as fragments and the fragment container were included in the navigation drawer activity.
What I want to do is reach these 4 fragments from the main activity, but in my main activity I can't reach the fragment container. What should I do? is there a solution to this problem?

Fragment Management - Navigation Drawer

I have a question about fragment management.
I have already programmed a drawer activity based on fragments. After every click on a menu item you will land on an empty fragment.
Now I want to have it so that no matter on which fragment I am on top the same is written. That's about 5 things that are there and depending on the action certain values on the fragment change whatever is there. Like a Information bar.
What options do I have and how do I do that?
Current project scope:
Main activity java + xml
and the Fragments + xml
The main activity is only for the Navigation Drawer. The app starts with a fragment.
How can I implement this?
in you main activity just commit which fragment you want to fill that first view with in onCreate.

How to disable drawer option in specific fragments of an activity

I have an activity with 5 fragments, I used drawer layout in my activity for a drawer, but I want to use(enable) the drawer in only fragment 2 and I want to disable the drawer option in remaining fragments.
Can any one help me how to do this?
Put two methods in the your activity, one to disable the drawer and one to enable it again, like so:
public void lockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
public void unlockDrawer() {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
Then in your fragments onCreateView(...) method put:
fragmentInteractionListener.lockDrawer();
for the fragments where the drawer should stay shut and for the fragments where the drawer should stay open put:
fragmentInteractionListener.unlockDrawer();
P.S: for tutorials in how to correctly implement a fragment interaction listener see:
https://developer.android.com/training/basics/fragments/communicating.html

Categories

Resources