Android Navigation core implemented through activity

I am developing my own application for Android, and I have come to the point where I have three different Activities: Activity A, Activity B and Activity C. Now I want to create a navigation box for navigating between them. I read the tutorial on the Android developers website, but they focused only on fragments. How are professional Android applications developed with only one activity, and all other screens are developed using fragments? If not, then why is it not documented how to implement the correct navigation box using "Actions"? Thanks for the help.

+8
android android-activity android-fragments android-navigation
source share
2 answers

You need to create a Base activity that does all the usual things of Drawer navigation . I will call this base of Activity as DrawerActivity , and all other Activity should expand this DrawerActivity . Thus, all Activity will have one instance of Drawer Layout .

Create a shared layout using DrawerLayout and put FrameLayout and ListView as a child

  <android.support.v4.widget.DrawerLayout> <FrameLayout android:id="@+id/activity_frame"/> <ListView android:id="@+id/left_drawer"/> </android.support.v4.widget.DrawerLayout> 

Now set this layout to onCreate() on DrawerActivity

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drawer_layout); // do other stuff to initialize drawer layout, add list items ……… ………. // add a listener to the drawer list view mLeftDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

}

Add Click Viewer

  private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { switch (position) { case 0: { Intent intent = new Intent(DrawerActivity.this, YourActivity.class); startActivity(intent); break; } default: break; } mDrawerLayout.closeDrawer(mLeftDrawerList); } } 

Finally, all other actions will expand this DrawerActivity

  public class MainActivity extends DrawerActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // don't set any content view here, since its already set in DrawerActivity FrameLayout frameLayout = (FrameLayout)findViewById(R.id.activity_frame); // inflate the custom activity layout LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View activityView = layoutInflater.inflate(R.layout.activity_main, null,false); // add the custom layout of this activity to frame layout. frameLayout.addView(activityView); // now you can do all your other stuffs } } 

Here you can see the full source https://gist.github.com/libinbensin/613dea436302d3015563

+16
source share

In each Office, you can have a NavigationDrawer populated with the same list of options.

-5
source share

All Articles