I know this question has been asked multiple times. But my concern is different.
Using Mr. Tamada Tutorial I've created a NavigaionActivity
and multiple fragments
to replace in FrameLayout
. It's working fine.
Now, after selecting any option in a fragment, another activity gets open.
I want the same Navigation menu in that Activity.
Navigation view -- fragments -- Activity (display navigation view here)
What I tried:
use the
xml
code of displayingNavigation view
in that activity. (DrawerLayout, CoordinatorLayout, AppBarLayout etc)Then in Activity.java, on click of menu item diverting to the Navigation Activity.
Code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
....>
<android.support.design.widget.CoordinatorLayout
....>
<android.support.design.widget.AppBarLayout
....>
<android.support.v7.widget.Toolbar
.... />
</android.support.design.widget.AppBarLayout>
<LinearLayout
.... /> <!-- main content of this Acitivity-->
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
.... />
</android.support.v4.widget.DrawerLayout>
Activity.java:
public void dashboard(MenuItem item) {
Bundle bundle = new Bundle();
bundle.putString("fragment", Constant.DASHBOARD_FRAGMENT);
UtilityClass.newActivity(this, NavigationActivity.class, bundle);
}
And handling the call on Navigation Activity. It is doing the task but code isn't re-usable
- Create a separate layout file for Navigation and include it in the Activity. But, this is replacing the main content. Here only included Navigation is visible.
Is there any other way to do it?
Downvoters - here the solution is..
xml
file which will haveDrawerLayout
andNavigationView
(one can use the xml given in Question, without the main content) -navigation.xml
As suggested in many answers "create a BaseActivity which
extends
AppCompatActivity. And inflatenavigation.xml
.In whichever
Activity
you wanna use thisNavigationMenu
, extend BaseActivity for that class.In
GraphActivity.xml
add theNavigationMenu
code. You can't just include thenavigation.xml
it will disable the current xml widgets.Done!
Try this:
NavigationDrawer is depricated....use something else for it is not much comfortable to use.
Plz Don't use it this way
Instead use Google recommended MasterDetailFlow Design
You can read how to implement this on
https://inducesmile.com/android/android-fragment-masterdetail-flow-tutorial-in-android-studio/