Activity re-created on bottom navigation view - android

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();
}

Related

Prevent fragment refreshing with bottom navbar

I have the following bottom navbar code to switch between 3 fragments:
public class MainActivity extends AppCompatActivity {
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_dashboard:
fragment = new DashboardFragment();
break;
case R.id.navigation_notifications:
fragment = new NotificationsFragment();
break;
}
return loadFragment(fragment);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadFragment(new HomeFragment());
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}
}
In the fragments there are RecyclerViews with lists. Every time I switch between the tabs (between fragments), it looks like the fragment is reloaded, and the lists jump to the top. I want to prevent that reloading so that the user stays on the same place in the list he viewed before switching fragments
The problem is that you are creating a new instance every time. You can cache the instance like:
private Fragment mHomeFragment = new HomeFragment();
private Fragment mDashboardFragment = new DashboardFragment();
private Fragment mNotificationsFragment = new NotificationsFragment();
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = mHomeFragment;
break;
case R.id.navigation_dashboard:
fragment = mDashboardFragment;
break;
case R.id.navigation_notifications:
fragment = mNotificationsFragment;
break;
}
return loadFragment(fragment);
}
As we could see, you are always replace your fragment when clicks on bottom navigation, replace means previous fragment removes and state cleans. The solution is do not create your fragment each time and use attach/detach method for showing actual fragment. Here is already described about these methods.

Bottom navigation view not working

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;
}
.......

Restrict loading two same fragments

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 to limit tap on BottomNavigatioView item to only once, to not reload fragment?

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;
}
});

Actionbar launch fragment

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;
}
}

Categories

Resources