简体   繁体   中英

How can I share a navigation drawer between multiple activities?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM