I am new to programming and I am creating an app with bottomNavigationView. In my Welcome activity I have my OnItemSelectedListener. But when I run the app in an android device and select an option it doesn't replace the fragment instead starts the Welcome Activity again and again.
This is the Welcome activity which keeps starting no matter what item I select from the BottonNavigationView.
public class Welcome extends AppCompatActivity {
private Context mContext = Welcome.this;
private static final String TAG = "Welcome";
BottomNavigationViewEx bottomNav;
private List<ListItems> listItems;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Log.d(TAG, "onCreate: starting");
setupBottomNavigationView();
Toast.makeText(this, "Starting", Toast.LENGTH_SHORT).show();
enableNavigation();
HomeFragment homeFragment = new HomeFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, homeFragment).commit();
}
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
bottomNav = findViewById(R.id.nav_bottom);
BottomNavigationViewHelper.setUpNavigationView(bottomNav);
}
private void enableNavigation(){
bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()){
case R.id.btnHome:
bottomNav.setSelectedItemId(R.id.btnHome);
HomeFragment homeFragment = new HomeFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, homeFragment).commit();
break;
case R.id.btnChats:
bottomNav.setSelectedItemId(R.id.btnChats);
ChatsFragment chatsFragment = new ChatsFragment();
android.support.v4.app.FragmentManager manager1 = getSupportFragmentManager();
manager1.beginTransaction().replace(R.id.container, chatsFragment).commit();
break;
case R.id.btnPost:
bottomNav.setSelectedItemId(R.id.btnPost);
PostsFragment postsFragment = new PostsFragment();
android.support.v4.app.FragmentManager manager2 = getSupportFragmentManager();
manager2.beginTransaction().replace(R.id.container, postsFragment).commit();
break;
case R.id.btnFavourites:
bottomNav.setSelectedItemId(R.id.btnFavourites);
FavouritesFragment favouritesFragment = new FavouritesFragment();
android.support.v4.app.FragmentManager manager3 = getSupportFragmentManager();
manager3.beginTransaction().replace(R.id.container, favouritesFragment).commit();
break;
case R.id.btnProfile:
bottomNav.setSelectedItemId(R.id.btnProfile);
ProfileFragment profileFragment = new ProfileFragment();
android.support.v4.app.FragmentManager manager4 = getSupportFragmentManager();
manager4.beginTransaction().replace(R.id.container, profileFragment).commit();
break;
}
return false;
}
});
}
}
This is the layout_welcome file
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.matt.mustjob.Home.Welcome">
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"/>
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/nav_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#431C60"
app:menu="@menu/menu"
app:itemIconTint="#fff"
app:itemTextColor="#FFF"/>
</android.support.design.widget.CoordinatorLayout>
A few things you need to change:
1) Don't use a ViewPager
as your Fragment
container, there's a way to use Fragments
with a ViewPager
and manage it in the Adapter
. Do a Google search for "viewpager with fragments example in android" to learn how to work with it. If you don't need a ViewPager
functionality, you can use a FrameLayout
as the container.
2) You don't need to save the Context
in an Activity
, you can always use getApplicationContext()
, getBaseContext()
or simply this
(or YourActivity.this
depending on the scope)
3) No need to call getSupportFragmentManager();
every time. You can call it once and assign the reference to a member in the Activity
:
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
manager = getSupportFragmentManager()
}
// Somewhere else:
manager.beginTransaction()...
4) You need to return true;
after you handle the onNavigationItemSelected
event, so instead of calling break;
after each choice, do like this:
...
case R.id.btnHome:
bottomNav.setSelectedItemId(R.id.btnHome);
HomeFragment homeFragment = new HomeFragment();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, homeFragment).commit();
return true; // This thing here
...