Android Studio - Navigating Activities -


i can't seem figure 1 out. have mainactivity , created secondactivity , thirdactivity want able navigate to.

i'm using bottomnavigation in mainactivity navigated between activities:

public class mainactivity extends appcompatactivity {  protected bottomnavigationview navigationview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);  bottomnavigationview.onnavigationitemselectedlistener monnavigationitemselectedlistener             = new bottomnavigationview.onnavigationitemselectedlistener() {          @override         public boolean onnavigationitemselected(@nonnull menuitem item) {             switch (item.getitemid()) {                 case r.id.navigation_menuitem1:                     intent intent = new intent(mainactivity.this, mainactivity.class);                     startactivity(intent);                     return true;                 case r.id.navigation_menuitem2:                     intent intent = new intent(mainactivity.this, secondactivity.class);                     startactivity(intent);                     return true;                 case r.id.navigation_menuitem3:                     intent intent = new intent(mainactivity.this, thirdactivity.class);                     startactivity(intent);                     return true;             }             return false;         }      };  navigationview = (bottomnavigationview) findviewbyid(r.id.navigation); navigationview.setonnavigationitemselectedlistener(monnavigationitemselectedlistener); 

any clue why it's not switching pages/activities?

edit: added these lines make work:

protected bottomnavigationview navigationview; 

and

navigationview = (bottomnavigationview) findviewbyid(r.id.navigation); navigationview.setonnavigationitemselectedlistener(monnavigationitemselectedlistener); 

thanks help!

just in case missed something, make sure didn't forget something:

create bottomnavigationview in xml of layout:

<android.support.design.widget.bottomnavigationview         android:id="@+id/navigation"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_marginend="0dp"         android:layout_marginstart="0dp"         android:background="?android:attr/windowbackground"         app:layout_constraintbottom_tobottomof="parent"         app:layout_constraintleft_toleftof="parent"         app:layout_constraintright_torightof="parent"         app:menu="@menu/navigation" /> 

create file here navigation.xml in menu resource folder. file used providing menuitems in bottomnavigationview

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android">     <item        android:id="@+id/navigation_menuitem1"        android:icon="@drawable/ic_home_black_24dp"        android:title="@string/menuitem1" />     <item        android:id="@+id/navigation_menuitem2"        android:icon="@drawable/ic_dashboard_black_24dp"        android:title="@string/menuitem2" />     <item        android:id="@+id/navigation_menuitem3"        android:icon="@drawable/ic_notifications_black_24dp"        android:title="@string/menuitem3" />   </menu> 

now lets set listener click events onnavigationitemselectedlistener , onnavigationitemreselectedlistener on menu items:

private bottomnavigationview.onnavigationitemselectedlistener monnavigationitemselectedlistener             = new bottomnavigationview.onnavigationitemselectedlistener() {          @override         public boolean onnavigationitemselected(@nonnull menuitem item) {              switch (item.getitemid()) {                 case r.id.navigation_menuitem1:                     return true;                  case r.id.navigation_menuitem2:                     return true;                  case r.id.navigation_menuitem3:                     return true;               }             return true;         }      };      private bottomnavigationview.onnavigationitemreselectedlistener monnavigationitemreselectedlistener = new bottomnavigationview.onnavigationitemreselectedlistener() {         @override         public void onnavigationitemreselected(@nonnull menuitem item) {              switch (item.getitemid()) {                  case r.id.navigation_menuitem1:                     log.d(tag, "navigation_menuitem1 reselected ===");                     break;                  case r.id.navigation_menuitem2:                     log.d(tag, "navigation_menuitem2 reselected ===");                     break;                  case r.id.navigation_menuitem3:                     log.d(tag, "navigation_menuitem3 reselected ===");                     break;              }          }     };  bottomnavigationview.setonnavigationitemselectedlistener(monnavigationitemselectedlistener);  bottomnavigationview.setonnavigationitemreselectedlistener(monnavigationitemreselectedlistener); 

edit add oncreate()

bottomnavigationview bottomnavigationview; bottomnavigationview = findviewbyid(r.id.navigation); 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -