Search code examples
androidandroiddesignsupportnavigationview

Android : Using NavigationView as a BaseActivity


I'm working on design support library and I would like to have "Same Navigation Drawer in different Activities". I tried extending MainActivity with following code, but doesn't work.

MainActivity.java

public class MainActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawer = (NavigationView) findViewById(R.id.main_drawer);
    mDrawer.setNavigationItemSelectedListener(this);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  
    mDrawerLayout.setDrawerListener(mDrawerToggle);

}
private void navigate(int mSelectedId) {
    Intent intent = null;
    if (mSelectedId == R.id.navigation_item_2) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
        intent = new Intent(this, SampleActivity.class);
        startActivity(intent);
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
}

SampleActivity.java

public class SecondActivity extends MainActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_second, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/main_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:itemIconTint="@color/colorAccent"
    app:itemTextColor="@color/colorTextSecondary"
    app:menu="@menu/menu_drawer" />


Solution

  • It won't work because in SampleActivity you override the content view by calling this:

    setContentView(R.layout.activity_second);
    

    By calling that, your SampleActivity will lose access to the NavigationView because setContentView() changed the layout instead of inflating it into your MainActivity's layout.

    The workaround to this is to provide a dedicated place (like a FrameLayout) and then inflate any subclass' layout to it.

    // Add these codes below the toolbar in your activity_main.xml
    
    <FrameLayout
        android:id="@+id/ly_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    And when extending the class, call this instead of setContentView():

    getLayoutInflater().inflate(R.layout.activity_second, findViewById(R.id.ly_content));