How to switch between Action bar tabs smoothly without reloading fragments every time
By : Mazel
Date : March 29 2020, 07:55 AM
like below fixes the issue I use a ViewPager that contains all my fragments. Every time a tab is selected I only switch the position in the Viewpager. code :
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int position = adapter.findItemPosition(this.id);
ViewPager viewPager = (YourActivity)getActivity().getViewPager(); // Convenience method
if (viewPager != null) {
viewPager.setCurrentItem(position, this.shouldScroll);
} else {
Log.d(getClass().getSimpleName(), "No pager available");
}
}
private final class TabPageChangedListener extends ViewPager.SimpleOnPageChangeListener {
@Override
public void onPageSelected(int position) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setSelectedNavigationItem(position);
} else {
Log.e(getClass().getSimpleName(),
"No actionbar available to change selected tab.");
}
}
}
|
How can I switch between two fragments, without recreating the fragments each time?
By : user7659923
Date : March 29 2020, 07:55 AM
around this issue After @meredrica pointed out that replace() destroys the fragments, I went back through the FragmentManager documentation. This is the solution I've come up with, that seems to be working. code :
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
android.support.v4.app.FragmentManager; fragmentManager = getSupportFragmentManager();
switch(position) {
case 0:
if(fragmentManager.findFragmentByTag("one") != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("one")).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.container, new OneFragment(), "one").commit();
}
if(fragmentManager.findFragmentByTag("two") != null){
//if the other fragment is visible, hide it.
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("two")).commit();
}
break;
case 1:
if(fragmentManager.findFragmentByTag("two") != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(fragmentManager.findFragmentByTag("two")).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.container, new TwoFragment(), "two").commit();
}
if(fragmentManager.findFragmentByTag("one") != null){
//if the other fragment is visible, hide it.
fragmentManager.beginTransaction().hide(fragmentManager.findFragmentByTag("one")).commit();
}
break;
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mNavTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void onDestroy() {
super.onDestroy();
FragmentManager fragmentManager = getSupportFragmentManager();
if(fragmentManager.findFragmentByTag("one") != null){
fragmentManager.beginTransaction().remove(fragmentManager.findFragmentByTag("one")).commit();
}
if(fragmentManager.findFragmentByTag("two") != null){
fragmentManager.beginTransaction().remove(fragmentManager.findFragmentByTag("two")).commit();
}
}
|
Fragments get loaded every time i switch tab, need efficient method
By : TheInternerd12
Date : March 29 2020, 07:55 AM
should help you out According to revision 4 of the Support Package, a method was added to ViewPager which allows you to specify the number of offscreen pages to use, rather than the default which is 1. so you can load page rightside when move. In your case, you want to specify 2, so that when you are on the third page, the first one is not destroyed, and vice-versa. code :
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);
|
Android - Do something every time I switch from fragments
By : Dhimas Aditya
Date : March 29 2020, 07:55 AM
Does that help Use a ViewPager.OnPageChangeListener and use InvalidateOptionsMenu() inside that like so: code :
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
currentFragment = position // use a field to store position
invalidateOptionsMenu();
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
});
|
How to switch between fragments without recreating fragments each time?
By : Ville
Date : March 29 2020, 07:55 AM
Any of those help The replace method destroys your fragments. One workaround is to set them to Visibility.GONE, another (less easy) method is to hold them in a variable. If you do that, make sure you don't leak memory left and right. this question answered by meredrica in here
|