Skip to content

Instantly share code, notes, and snippets.

@maydin
Created March 9, 2017 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maydin/450f500e34c00556d232c1328133ced8 to your computer and use it in GitHub Desktop.
Save maydin/450f500e34c00556d232c1328133ced8 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
public static final String FRAGMENT_VIEWPAGER = "FRAGMENT_VIEWPAGER";
public static final String FRAGMENT_FIRST = "FRAGMENT_FIRST";
public static final String FRAGMENT_SECOND = "FRAGMENT_SECOND";
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
replaceFragment(FirstFragment.newInstance(),FRAGMENT_FIRST);
return true;
case R.id.navigation_dashboard:
replaceFragment(SecondFragment.newInstance(),FRAGMENT_SECOND);
return true;
case R.id.navigation_notifications:
replaceFragment(ViewPagerFragment.newInstance(),FRAGMENT_VIEWPAGER);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_container, FirstFragment.newInstance(), FRAGMENT_FIRST)
.commit();
}
private void replaceFragment(Fragment newFragment, String tag) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, newFragment, tag)
.commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment