How can I use onBackPressed in BottonNavigationView to select an item? For example, if I press item 2 I go to fragment number 2, but when I push back, I go back to fragment number 1 and item 2 is still selected, item 1 is not selected which is the one corresponding to fragment 1 ...
This is my code
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
}
UPDATE
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content, new MapFragment()).commit();
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomViewBar);
bottomNavigationViewEx.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
bottomNavigationViewEx.setSelectedItemId(R.id.mapa);
MenuItem item = bottomNavigationViewEx.getMenu().getItem(0);
if (!item.isChecked()) item.setChecked(true);
}
private BottomNavigationViewEx.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.mapa:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.taxi:
transaction.replace(R.id.content, new TaxiFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.help:
transaction.replace(R.id.content, new HelpFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
case R.id.go:
transaction.replace(R.id.content, new GoFragment());
transaction.addToBackStack(null);
getSupportFragmentManager().popBackStack();
transaction.commit();
return true;
}
return false;
}
};
#Override
public void onBackPressed () {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment (mCurrentItem);
} else {
super.onBackPressed();
}
}
private void showFragment(int currentItem) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
transaction.replace(R.id.content, new TaxiFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 2:
transaction.replace(R.id.content, new HelpFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 3:
transaction.replace(R.id.content, new GoFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
}
}
}
I have this now, but I don't know if is correct
Keep a variable to denote which Fragment is your current. Inside onBackPressed, check what is the current Fragment, and decide where do you want to navigate to.
#Override
public void onBackPressed() {
if (mCurrentItem > 0) {
mCurrentItem--;
showFragment(mCurrentItem);
} else {
super.onBackPressed();
}
}
Your showFragment(mCurrentItem) can show different page based on the value, or however you'd like it to.
private void showFragment(int currentItem) {
switch (currentItem) {
case 0:
transaction.replace(R.id.content, new MapFragment());
getSupportFragmentManager().popBackStack();
transaction.commit();
break;
case 1:
...
}
}
Then inside your showFragment(currentItem) method, add this code to select the item on BottomNavigationView:
MenuItem item = mBottomNavigationView.getMenu().getItem(mCurrentItem);
if (!item.isChecked()) item.setChecked(true);
Solve my problem by adding this line inside the class of my corresponding fragment in the onCreateView method, so when pressing back the item is selected.
BottomNavigationViewEx navigation =
getActivity().findViewById(R.id.YourNavigationId);
Menu drawer_menu = navigation.getMenu();
MenuItem menuItem;
menuItem = drawer_menu.findItem(R.id.YourItemId);
if(!menuItem.isChecked())
{
menuItem.setChecked(true);
}
Related
I have made the code to do that it loads fragment on clicking the items in the bottom navigation but when I click on it they don't work. I tried many times to check whats the problem even though Android Studio couldn't find it bu the app isn't working. I don't know whats the issue.
My main activity -
public class MainActivity extends AppCompatActivity {
BottomNavigationView btm_view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Chats());
fragmentTransaction.commit();
btm_view = findViewById(R.id.bottom_view);
btm_view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.camera) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Camera());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.chats) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Chats());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.status) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Status());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.calls) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Calls());
fragmentTransaction.commit();
} else if (item.getItemId() == R.id.settings) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.container, new Settings());
fragmentTransaction.commit();
}
return false;
}
});
}
}
one of my fragments -
public class Chats extends Fragment {
public Chats() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_chats, container, false);
}
}
Try this way
BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.chats:
openFragment(Chat.newInstance());
return true;
case R.id.camera:
openFragment(Camera.newInstance());
return true;
case R.id.status:
openFragment(Status.newInstance("", ""));
return true;
}
return false;
}
};
At first use replace(R.id.container, fragment) instead of add.
Optimise Code
Utilise setOnNavigationItemSelectedListener. Try this way
Set a listener that will be notified when a bottom navigation item is
selected.
btm_view.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
openFragment(Camera.newInstance());
}
public void openFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.camera:
openFragment(Camera.newInstance());
return true;
case R.id.chats:
openFragment(Chats.newInstance());
return true;
case R.id.status:
openFragment(Status.newInstance());
return true;
case R.id.calls:
openFragment(Calls.newInstance());
return true;
case R.id.settings:
openFragment(Settings.newInstance());
return true;
}
return false;
}
};
}
Your Chats Fragment will be
public class Chats extends Fragment {
public Chats() {
// Required empty public constructor
}
public static Chats newInstance() {
Chats fragment = new Chats();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_chats, container, false);
}
}
This is my code I have created a bottom navigation view and I am connected the items to fragment separately. When I click the the navigation item more time the fragment load again again.
I don't know what is the problem here.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment SelectedFragment=null;
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.commit();
switch (item.getItemId()) {
case R.id.navigation_home:
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
case R.id.navigation_dashboard:
HomeFragment1 fragment1 = new HomeFragment1();
ft.replace(R.id.fragment_container, fragment1);
return true;
case R.id.navigation_notifications:
setTitle("Safe11");
HomeFragment2 fragment2 = new HomeFragment2();
ft.replace(R.id.fragment_container, fragment2);
return true;
case R.id.navigation_notification:
setTitle("More");
HomeFragment3 fragment3 = new HomeFragment3();
ft.replace(R.id.fragment_container, fragment3);
return true;
}
return false;
}
};
}
Try to change your code like this:
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.menu_home:
HomeFragment homeFragment = new HomeFragment();
fragmentTransaction.replace(R.id.frameContentPlace, homeFragment, "HomeFragment").addToBackStack("HomeFragment");
break;
case R.id.menu_profile:
HomeFragment1 fragment1 = new HomeFragment1();
fragmentTransaction.replace(R.id.frameContentPlace, fragment1).addToBackStack("HomeFragment1");
break;
case R.id.menu_bookings:
HomeFragment2 fragment2 = new HomeFragment2();
fragmentTransaction.replace(R.id.frameContentPlace, fragment2).addToBackStack("HomeFragment2");
break;
}
fragmentTransaction.commit();
return true;
}
When you adding fragment use tag. So write
ft.replace(R.id.fragment_container, fragment, "MyFragment);
instead of
ft.replace(R.id.fragment_container, fragment);
And check if this fragment is visible for not loading this fragment again
switch (item.getItemId())
{
case R.id.navigation_home:
HomeFragment fragment = (HomeFragment)getFragmentManager().findFragmentByTag("MyFragment");
if (fragment != null && !fragment.isVisible())
{
setTitle("Playing11");
HomeFragment fragment = new HomeFragment();
ft.replace(R.id.fragment_container, fragment);
return true;
}
}
Hi i'm making an app where i'm using fragments with bottom navigation however when i click on home button it doesnt load home fragment instead it loads the second fragment and moreover when i start the app home fragment doesnt load by default i know it may sound naive but i'm having this problem plaese if someone may guide me
my code for fragment navigation
public class MainActivity extends AppCompatActivity {
FrameLayout frameLayout;
FragmentManager fragmentManager;
Fragment fragment;
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//BottomNavigationView b=findViewById(R.id.bottom_nav);
fragmentManager = getSupportFragmentManager();
if(findViewById(R.id.frame_container)!=null){
if(savedInstanceState!=null){
return;
}
/*HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, homeFragment,null);
transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
//transaction.commit();
}
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
//final SeekBar sb = (SeekBar) findViewById(R.id.sb);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
});
}
private void loadFragment(Fragment fragment) {
// HomeFragment homeFragment = new HomeFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment,null);
transaction.addToBackStack(null);
/* Comment this line and it should work!*/
//transaction.addToBackStack(null);
transaction.commit();
// load fragment
//HomeFragment homeFragment = new HomeFragment();
}
}
You just forgot to return true; inside case R.id.navigation_home: check it
Try this
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_feed:
fragment = new FeedFragment();
loadFragment(fragment);
return true;
case R.id.navigation_event:
fragment = new EventsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_nearby:
fragment = new NearbyFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new ProfileFragment();
loadFragment(fragment);
return true;
}
return false;
}
});
My MainActivity is the Google Maps Activity and I have 2 fragments called "LoginForm" and "appSettings". I can switch from the MainActivity to the 2 fragments, but I can't switch back to MainActivity. I have tried fragment.addToBackStack() but it doesn't do anything. Any idea? Here is the code from the Navigation Drawer.
public void selectedDrawerItem(MenuItem menuItem){
Class fragmentClass = null;
FrameLayout frameLayoutMap = (FrameLayout) findViewById(R.id.main_container_login);
FragmentManager fragmentManager = getSupportFragmentManager();
// For every new Fragment, add in Switch statement.
switch(menuItem.getItemId()) {
case R.id.nav_geomap:
fragmentClass = MapsActivity.class;
break;
case R.id.nav_login:
Log.d(TAG, "Accessed fragment: menuItem nav_login");
//frameLayoutMap.removeAllViews();
fragmentClass = LoginForm.class;
break;
case R.id.nav_settings:
Log.d(TAG, "Accessed fragment: menuItem nav_settings");
// frameLayoutMap.removeAllViews();
fragmentClass = appSettings.class;
break;
}
try
{
fragment = (Fragment) fragmentClass.newInstance();
}
catch (Exception e){
e.printStackTrace();
}
fragmentManager.beginTransaction().replace(R.id.map, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawerLayout.closeDrawers();
}
try this
public void addPage(Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.content_main, fragment);
ft.addToBackStack(null);
ft.commit();
}
public void forwardPage(final Fragment fragment) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_main, fragment);
ft.addToBackStack(null);
ft.commit();
}
"Statement not reachable under the switch case for signing out using the button in menu item. "
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.sign_out_menu:
AuthUI.getInstance().signOut(this); //NOT REACHABLE
return true;
default:
return super.onOptionsItemSelected(item);
}
}
write this line at the end of method
"return super.onOptionsItemSelected(item);"
Remove the first line in the method onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item); // Remove this line
switch(item.getItemId()){
}
}
Because you have a return statement in your code, everything under the 1st return statement which is not part of the switch is not reachable
you can use either return super.onOptionsItemSelected(item); or just true; after ending switch case, don't forget break; statement too.
int id = item.getItemId();
switch (id) {
case R.id.id1:
Fragment1 fragment1 = new Fragment1();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(((ViewGroup)getView().getParent()).getId() , fragment1);
fragmentTransaction.commit();
break;
case R.id.id2:
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 = fragmentManager1.beginTransaction();
fragmentTransaction1.replace(((ViewGroup)getView().getParent()).getId() , fragment2);
fragmentTransaction1.commit();
break;
}
return true;