-1

Hi I'm working on a school project for my High School graduation. The project consist of an Android app wich has as the MainActivity a Navigation Drawer. Now,I've learnt thousands of question about this argument, but all the answers were all about using fragments. I do not want to use fragments, so how can I share the Navigation Drawer between different activities?

NB: 1) I've used the stock Navigation Drawer provided by Android Studio.

2) I do not want to share the Toolbar of the MainActivity, but just the Navigation Drawer.

Thanks.

Matts
  • 11
  • 2

1 Answers1

0

You need to create BaseActivity with Drawer and all child activity will be extend that BaseActivity

public class BaseActivity extends AppCompatActivity  {    
    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(R.layout.activity_base);
        // Base activity view with navigation drawer

        mainContener = (RelativeLayout) findViewById(R.id.mainContener);
        // MainCOntener where you want to add your child activity view
        // with toolbar and all
        getLayoutInflater().inflate(layoutResID, mainLayout);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setOnClickListener(this);

        findViewById(R.id.linearLayout_home).setOnClickListener(this);
        ...
        ...

        getDrawerData();
    }

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

    }


  }

And Create Child Activity Like this

public class Home extends BaseActivity {


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

Everytime when setContentView(); call from child activity that view (or Layout like R.layout.home) will be inflate in baseactivity's Contener (like R.layout.base_activity )'s ViewGroup

For further information you can use Google-IO app code they also use BaseActivity Concept https://github.com/google/iosched

Jaydeep purohit
  • 1,536
  • 17
  • 20
  • you should remove `setContentView(R.layout.activity_base);` from the base activity as it would result in the code in your custom `setContentview` be executed twice – Dodge Apr 19 '16 at 11:35