I have a navigation drawer project with some fragments, now I want to call an activity from the menu.
The fragments are loaded into a framelayout and replaced with a new fragment on click event menu with a transaction.
When I click on the menu item I call this code:
fragment = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
Now if I call an activity with this code:
Intent intent = new Intent(MainActivity.this, asd.class);
startActivity(intent);
the activity override the entire navigation drawer because I don't replace the existing fragment into frame_container with the new activity.
How can I do this? Thanks!
Fragment can be added to the container. Activity hosts a fragment. What is happening is you are Navigating to a different Activity. You have a drawer in MainActivity not asd.
If you want a drawer in asd also create a BaseActivtiy that has NavigationDrawer and extend from it.
Your Navigation Drawer and the FrameLayout, all are inside one Activity where in you can replace the fragments and at the same time drawer will be visible.
However, when you start a new Activity, a new screen appears which do not have the implementation of the Navigation Drawer as it belongs to your first Activity only.
Use #Raghunandan's way and create a new BaseActivity which is extended by both of your Activites (one containing FrameLayout and the other one) or in case it is possible to use another Fragment instead of an Activity then create a new fragment then you can simply replace the new one in your FrameLayout and you will have the NavigationDrawer as well. :)
Related
I have an issue with my Android app that uses Navigation and ViewPager2.
I've the main Activity which implements a Navigation Drawer and has its own Navigation Graph and it works fine with the rest of the application.
Then I have a second Activity that has a ViewPager2 which displays 3 different Fragments. Now the problem is that I need to navigate from the Main Activity to the Pager activity keeping the original Navigation infrastructure. I know that each activity has it's own Navigation Graph, but the second activity has a ViewPager2 control so that means it has 3 Fragments and I cannot specify a startDestination in the new Navigation Graph .... because there are 3 and here is the problem. I can display the new activity but the Application Bar with the Back button which would navigate to the original activity is not displayed. Any solution ?
You can not handle fragment of viewpager through nav graph, but you can create parent fragment called ViewPagerTabsHolderFragment and inside it, you will set up the view pager adapter fragments then you can set the nav graph start destination to this ViewPagerTabsHolderFragment
I have 5 Fragments and of course my .MainActivity.
MainActivity
FragOne
FragTwo
---
FragFive
and then:
activity_main.xml
app_bar_main.xml
content_main.xml
nav_header_main.xml
lay1.xml
lay2.xml
---
lay5.xml
When the application is loaded it's a blank screen but then obviously when I click the navigation bar the pages will load.
My question is, is there anyway I can use a fragment as my launcher page, because if I tried making a home page on any other activity like content_main or acivity_main, obviously then it would show on every page which I don't want.
But then if I made a new activity and set that as the launcher,obviously that would work but then my navigation bar would be missing I presume? What's the best way to go about this?
What you want is not that big a problem. Just load a fragment using SupportFragmentManager in your MainActivity. If this Fragment is a one time view only (before you change to your other fragments using Navigation Drawer), then a simple loading of your launcher Fragment in the MainActivity is enough.
Example for loading it once in MainActivity:
In your onCreate method in MainActivity :
///////////Setting up Fragment as Starting Page////////////
LauncherFragment fragment = new LauncherFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment,"launcher");
fragmentTransaction.commit();
This Launcher Fragment won't be seen after you change to other Fragments using NavigationDrawer (unless you call it somewhere else, which according to your question you don't want)
If instead of using separate LauncherFragment you decide you want to load FragOne itself, you will need to manually highlight your Drawer's First item (FragOne) after setting up FragOne
navigationView.getMenu().getItem(0).setChecked(true); ////Set Navigation drawer manually
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);
How can I completely remove the ActionBar from Certain Fragments. I want to remove it not just to hide it. I have Actionbar Tab Navigation. From that i added a new Fragmnt which didn't need actionBar. So i need to remove it. I can't hide it because when i pressed back button and moved to Tabs section, i need to show Action bar again.
This is how i am replacing Fragments.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container, fragment)
.addToBackStack(null).commit();
The action bar is not part of your fragment. It's part of your activity.
You can create a new Activity with a theme Theme.Holo.Light.NoActionBar and open this Activity with :
getActivity().startActivity(new Intent(getActivity(), SecondActivity.class));
And inside your second activity you can display the fragment you want.
I recommend you this post to understand more the differences between Fragment and Activity: https://stackoverflow.com/a/10515807/3112836
If you're not using actionbar.hide() just because you have to show Action bar in other fragment, than I think you should give it a try by using :
actionbar.hide() inside onAttach method of your fragment and actionBar.show() inside your onDestroy() method.
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.