Materialdrawer: how to use one Drawer in all acitivity ?

Created on 8 Apr 2016  路  5Comments  路  Source: mikepenz/MaterialDrawer

hi @mikepenz i want use one drawer in all activity
but i dont want copy and paste codes in all activity
can you help me ?

question

Most helpful comment

@kakai248 thanks for answering.

@armoun the answer given by @kakai248 is the basic implemenation what to do. It's no running sample. If you need help on how to get a base activity working you can try to search on stackoverflow or other sites, as this is nothing lib specific

All 5 comments

You can either make an abstract activity that implements the drawer and then make the other activities extend from that one or have one activity and use fragments.

@kakai248 can you show me in example ? thanks

For extending you could do something like this:

public abstract class BaseActivity extends AppCompatActivity {
    protected Drawer drawer;

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawer = new DrawerBuilder()
                .withActivity(this)
                .addItems(...)
                .build();
    }
}

And then each activity extends the base activity:

public class MainActivity extends BaseActivity {
    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // you can access the drawer here because it was initialized in super
    }
}

For fragments you have one activity that uses the drawer and then the actual content is displayed on a fragment. For this option you better go check the Android documentation.

@kakai248 thanks
but not work
can you show me in a sample ?
thank u so much

@kakai248 thanks for answering.

@armoun the answer given by @kakai248 is the basic implemenation what to do. It's no running sample. If you need help on how to get a base activity working you can try to search on stackoverflow or other sites, as this is nothing lib specific

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Meeks91 picture Meeks91  路  3Comments

HanderWei picture HanderWei  路  3Comments

pranjal-joshi picture pranjal-joshi  路  3Comments

singhalavi picture singhalavi  路  4Comments

AlexMercier picture AlexMercier  路  3Comments