How can I launch a fragment from onOptionsItemsSelected?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_support:
Fragment f = new SupportFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(android.R.id.content, f).commit();
Toast display = Toast.makeText(this, "Settings", 10);
display.show();
case R.id.action_guide:
Toast display1 = Toast.makeText(this, "Guide", 10);
display1.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Im trying to launch the support fragment.
Thanks
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.action_support:
fragment = new Home();
case 1:
fragment = new NotesList();
default:
fragment = new defaultPageOrAnyOtheOption();
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
return true;
} // onOptionsItemSelected
Try it
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_support:
//launch Support Fragment
// Fragment f = new SupportFragment();
// replace(android.R.id.content, f);
Toast display = Toast.makeText(this, "Settings", 10);
display.show();
return true;
case R.id.action_guide:
Toast display1 = Toast.makeText(this, "Guide", 10);
display1.show();
return true;
}
}
Related
Following is my code for bottom navigation view item selected
public static void setupBottomNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
Log.d(TAG, "setupBottomNavigationView: Setting up NavigationView1");
bottomNavigationViewEx.enableAnimation(false);
bottomNavigationViewEx.enableItemShiftingMode(false);
bottomNavigationViewEx.enableShiftingMode(false);
bottomNavigationViewEx.setTextVisibility(false);
}
public static void enableNavigation(final Context context, BottomNavigationViewEx view){
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()){
case R.id.nav_home:
fragment = new FragmentMarker();
loadFragment(fragment);
return true;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
loadFragment(fragment);
return true;
case R.id.nav_blog:
fragment = new FragmentBlog();
loadFragment(fragment);
return true;
case R.id.nav_notification:
fragment = new FragmentNotification();
loadFragment(fragment);
return true;
case R.id.nav_account:
fragment = new FragmentAccount();
loadFragment(fragment);
return true;
}
return false;
}
});
}
Is there any way to display fragments on bottom navigation bar selected without re-creating activity?
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {
case R.id.nav_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.nav_bookmark:
fragment = new BookmarkFragment();
loadFragment(fragment);
return true;
case R.id.nav_blog:
fragment = new BlogFragment();
loadFragment(fragment);
return true;
}
return false;
}
};
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
My bottom navigation view not working. When i click on items my fragment in not loading.
This is my activity for bottom navigation view
public static void enableNavigation(Context context, final BottomNavigationViewEx view, final FragmentManager supportFragmentManager){
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()){
case R.id.nav_home:
fragment = new FragmentMarker();
loadFragment(fragment);
return true;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
loadFragment(fragment);
return true;
case R.id.nav_blog:
fragment = new FragmentBlog();
loadFragment(fragment);
return true;
case R.id.nav_notification:
fragment = new FragmentNotification();
loadFragment(fragment);
return true;
}
return false;
}
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = fragment.getFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
I`am using this library link
Try
Fragment fragment;
boolean valToReturn = false
switch (item.getItemId()){
case R.id.nav_home:
fragment = new FragmentMarker();
valToReturn = true;
break;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
valToReturn = true;
break;
case R.id.nav_blog:
fragment = new FragmentBlog();
valToReturn = true;
break;
case R.id.nav_notification:
fragment = new FragmentNotification();
valToReturn = true;
break;
}
loadFragment(fragment);
return valToReturn;
The Java break is used to switch statement. It breaks the current flow
of the program at specified condition.
You miss to add break; .
DEMO STRUCTURE
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
fragment = FragmentMarker();
loadFragment(fragment);
break;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
loadFragment(fragment);
break;
}
.......
I'm using BottomNavigationView. When I click on an option twice then same fragment also loaded twice. How to prevent loading same fragment?
btnNavBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_item1:
loadFragment(fragmentManager, new HomeFragment(), "Home");
break;
case R.id.action_item2:
loadFragment(fragmentManager, new SearchFragment(), "Search");
break;
case R.id.action_item3:
loadFragment(fragmentManager, new AccountFragment(), "Account");
break;
}
return true;
}
});
public static void loadFragment(FragmentManager fragmentManager, Fragment fragment, String tag) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frameLayoutContainer, fragment);
fragmentTransaction.commit();
}
the easiest approach is tracking current fragment which is displayed at BottomNavigationView:
int currentTabSelected;
...
btnNavBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_item1:
if(currentTabSelected == R.id.action_item1) return true;
loadFragment(fragmentManager, new HomeFragment(), "Home");
break;
case R.id.action_item2:
if(currentTabSelected == R.id.action_item2) return true;
loadFragment(fragmentManager, new SearchFragment(), "Search");
break;
case R.id.action_item3:
if(currentTabSelected == R.id.action_item3) return true;
loadFragment(fragmentManager, new AccountFragment(), "Account");
break;
}
currentTabSelected = item.getItemId();
return true;
}
});
How can I limit a tap on a BottomNavigatioView item to just once, so that my mapfragment doesn't have to reload itself when someone spamms the item?
My current Solution is this one:
fragmentManager = getSupportFragmentManager();
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
Fragment currentFragment = fragmentManager.findFragmentById(R.id.main_container);
int id = item.getItemId();
switch (id){
case R.id.World:
if (!(currentFragment instanceof MapsFragment)) {
fragment = new MapsFragment();
}
break;
case R.id.Friends:
fragment = new FriendsFragment();
break;
case R.id.Chats:
fragment = new You_Fragment();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
but it doesn't work since the fragment throws a Nullpointer.
What can i change to achieve the above?
Try this! Worked for me:
fragmentManager = getSupportFragmentManager();
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.main_container);
int id = item.getItemId();
switch (id){
case R.id.World:
if (currentFragment instanceof MapsFragment) {
return false;
} else {
fragment = new MapsFragment();
}
break;
case R.id.Friends:
fragment = new FriendsFragment();
break;
case R.id.Chats:
fragment = new You_Fragment();
break;
}
final FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment).commit();
return true;
}
});
I want there to be a way to get back to the MainActivity from the nav when I switch fragments. The way I was trying to do it before was causing the app to crash. The main screen is the main activity.
public void selectDrawerItem(MenuItem menuItem)
{
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId())
{
case R.id.nav_inspi:
fragmentClass = InFragment.class;
break;
default:
fragmentClass = InFragment.class;
break;
}
try
{
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fLayout_, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
zDrawerLayout.closeDrawers();
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
zDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
if (zDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
Try this change in your code
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fLayout_, fragment).addToBackStack(null).commit();