I'm having issues with fragment replacing implementing a navigation drawer.
The problem is layout overlapping, but it happens only if I let the application on the background for a long time (I asume after the activity goes to Stop).
This is the code I'm using for changing fragments. I'm not doing anything in onStop method
(Should I be doing something?)
#Override
public void onNavigationDrawerItemSelected(int position) {
if(lastPosition == position){
return;
}
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment lastFragment = fragmentManager.findFragmentByTag( lastTag );
Fragment fragment;
if ( lastFragment != null ) {
transaction.hide( lastFragment );
}
switch (position) {
case 0:
lastTag = "miPerfil";
lastPosition = 0;
fragment = fragmentManager.findFragmentByTag("miPerfil");
if(fragment != null) {
transaction.remove(fragment);
}
transaction.add(R.id.container, MiPerfilFragment.newInstance(position + 1),"miPerfil");
break;
case 1:
lastTag = "misReportes";
lastPosition = 1;
fragment = fragmentManager.findFragmentByTag("misReportes");
if(fragment != null) {
transaction.remove(fragment);
}
transaction.add(R.id.container, MisReportesFragment.newInstance(position + 1),"misReportes");
break;
case 2:
lastTag = "mapFragment";
lastPosition = 2;
fragment = fragmentManager.findFragmentByTag("mapFragment");
if(fragment != null) {
transaction.show(fragment);
}else{
transaction.add(R.id.container, MapFragment.newInstance(position + 1),"mapFragment");
}
break;
case 3:
moveTaskToBack(true);
return;
}
transaction.addToBackStack( lastTag ).commit();
}
Thanks.
there is two solution that you can try to overcome from your problem of overlapping fragment.
try (1) set a background color to your fragment in xml file.
if it does not work,
try (2) fixing this by writing your own code to remove any existing fragments before committing a new one.
for example:
fragmentManager = getFragmentManager();
ft = fragmentManager.beginTransaction();
mbFragment = new SettingsManageBooksFragment();
ft.replace(R.id.setting_detail_container2, mbFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonSettingsManageBooks:
if (mPurchaseHistory == true) {
ft.remove(phFragment);
Log.d(TAG, "REMOVING PURCHASE HISTORY FRAG");
} else if (mAudio == true) {
ft.remove(aFragment);
Log.d(TAG, "REMOVING AUDIO FRAG");
} else if (mRestore == true) {
ft.remove(rFragment);
Log.d(TAG, "REMOVING RESTORE FRAG");
} else if (mCopyright == true) {
ft.remove(cFragment);
Log.d(TAG, "REMOVING COPYRIGHT FRAG");
} else if (mAbout == true) {
ft.remove(abFragment);
Log.d(TAG, "REMOVING ABOUT FRAG");
}
ft = fragmentManager.beginTransaction();
mbFragment = new SettingsManageBooksFragment();
ft.replace(R.id.setting_detail_container2, mbFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
mManageBooks = true;
mPurchaseHistory = false;
mAudio = false;
mRestore = false;
mCopyright = false;
mAbout = false;
break;
hope it will helps....
if you use first solution You may also come up with problems where the fragment behind your new fragment is still clickable. If this happens then just make the parent view of the new fragment clickable..
Related
I am using bottom navigation drawer to switch between fragments, the problem is that every time I switch back to a fragment it gets recreated.
How can I save the state of fragment and resume it when switched back to it?
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
#Override
public void onTabSelected(int position) {
if (position==0){
loadFragment(new Daily());
}
if (position==1){
loadFragment(new Trending());
}
if (position==2){
loadFragment(new Random());
}
}
#Override
public void onTabUnselected(int position) {
}
#Override
public void onTabReselected(int position) {
}
});
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.attach( fragment);
transaction.addToBackStack(null);
transaction.commit();
}
The issue is that you are always creating a new Fragment on any onTabSelected. So in order to fix it, you need to work with your FragmentManager.
Possible solution: use the add and show/hide methods.
Example:
private static final String DAILY_TAG = BuildConfig.APPLICATION_ID + ".DAILY_TAG";
private static final String TRENDING_TAG = BuildConfig.APPLICATION_ID + ".TRENDING_TAG";
private static final String RANDOM_TAG = BuildConfig.APPLICATION_ID + ".RANDOM_TAG";
public void onTabSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (position == 0) {
hideFragment(TRENDING_TAG)
hideFragment(RANDOM_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(DAILY_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Daily(), DAILY_TAG)
}
transaction.commitNow()
} else if (position == 1) {
hideFragment(DAILY_TAG)
hideFragment(RANDOM_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(TRENDING_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Trending(), TRENDING_TAG)
}
transaction.commitNow()
} else {
hideFragment(TRENDING_TAG)
hideFragment(DAILY_TAG)
Fragment fragment = fragmentManager.findFragmentByTag(RANDOM_TAG);
FragmentTransaction transaction = fragmentManager.beginTransaction()
if (fragment != null) {
transaction.show(fragment)
} else {
transaction.add(content.id, new Random(), RANDOM_TAG)
}
transaction.commitNow()
}
fragments.put(position, fragment);
loadFragment(fragment);
}
private void hideFragment(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager()
Fragment currentFragment = fragmentManager.findFragmentByTag(tag)
if (currentFragment != null) {
fragmentManager.beginTransaction().hide(currentFragment).commitNow()
}
}
PS - The code can be optimized.
In my main activity I am displaying several fragments depending on option chosen by the user. When screen is rotated and then fragment changed, the previous fragment, one that was visible while rotating screen, is transparent and still visible behind or over new one. I am adding fragments using add() method and then removing old ones with remove(). I am aware that simply using replace() would fix my problem but my main fragment contains many network and database calls on create and I would like to remember its state rather than make many unnecessary calls.
Why does my problem even occur when I am removing fragments other than the main one? How can I fix it?
Here is how I am setting my fragments:
public void setFragment(FragmentEnum fragmentEnum, #Nullable Bundle bundle) {
Fragment oldFragment = getCurrentFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
currentFragment = fragmentEnum;
mMapFragment = (MainMapFragment) fragmentManager.findFragmentByTag(MainMapFragment.TAG);
if (mMapFragment == null) {
mMapFragment = MainMapFragment.newInstance(bundle);
}
if (fragmentEnum == FragmentEnum.MAP) {
if (!mMapFragment.isAdded()) {
ft.add(R.id.container_contents, mMapFragment, MainMapFragment.TAG);
}
if (oldFragment != null && !(oldFragment instanceof MainMapFragment)) {
ft.remove(oldFragment);
}
ft.show(mMapFragment);
if (mNavigationDrawerFragment != null) mNavigationDrawerFragment.unselectItems();
} else {
ft.hide(mMapFragment);
Fragment newFragment = null;
String tag = null;
switch (fragmentEnum) {
case LIST:
newFragment = MainListFragment.newInstance();
tag = MainListFragment.TAG;
break;
case FAVORITE:
newFragment = MainFavoriteFragment.newInstance();
tag = MainFavoriteFragment.TAG;
break;
case ADD:
newFragment = MainAddFragment.newInstance();
tag = MainAddFragment.TAG;
break;
case USER:
newFragment = MainUserFragment.newInstance();
tag = MainUserFragment.TAG;
break;
}
ft.add(R.id.container_contents, newFragment, tag);
if (oldFragment != null && !(oldFragment instanceof MainMapFragment)) {
ft.remove(oldFragment);
}
ft.show(newFragment);
}
ft.commit();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(SAVE_STATE_CURR_FRG, currentFragment);
}
onCreate(){
...
if (savedInstanceState != null) {
FragmentEnum savedFragmentEnum = (FragmentEnum) savedInstanceState.getSerializable(SAVE_STATE_CURR_FRG);
setFragment(savedFragmentEnum, null);
}
...
}
I know this question has been asked so many times but I have tried every thing which was answered
like
getSupportFragmentManager().executePendingTransactions();
used fragmentTransaction.replace
my problem is when I use findFragmentByTag, it works at one place and does not work at other place.
I am not getting where I am doing wrong because findFragmentByTag return fragment in R.id.action_clean case but does not work in R.id.action_cart case.
my code is
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_cart: // don't work in this case
if (Cart.getProductCount(this) > 0) {
if (getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()) != null) {
if (getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()).isVisible()) {
Log.v("visible","visible");
}
Log.v("visible2","not null");
Toast.makeText(this, "Cart is not empty", Toast.LENGTH_SHORT).show();
}
else{
changeFragment(new CartFragment());
}
}else{
Toast.makeText(this,"Cart is empty",Toast.LENGTH_SHORT).show();
}
break;
case R.id.action_login:
changeFragment(new LoginFragment());
// Toast.makeText(this,"login",Toast.LENGTH_SHORT).show();
break;
case R.id.action_signup:
changeFragment(new UserDetailFragment());
//Toast.makeText(this,"sign up",Toast.LENGTH_SHORT).show();
break;
case R.id.action_clean: // work here
if(Cart.clearCart(this)){
Toast.makeText(this,"All items has been removed from Cart",Toast.LENGTH_SHORT).show();
updateCartCount();
if (getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()) != null
&& getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()).isVisible()) {
removeFragment(getSupportFragmentManager().findFragmentByTag(CartFragment.class.getName()));
}
}else Toast.makeText(this,"Cart can not be cleaned",Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
and here is the changeFragment
#Override
public void changeFragment(Fragment fragment, CustomDictionary... dictionary) {
FragmentTransaction ft= getSupportFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
if (dictionary != null && dictionary.length > 0) {
for (CustomDictionary dic : dictionary) {
bundle.putString(dic.getKey(),dic.getValue());
}
}
fragment.setArguments(bundle);
ft.setCustomAnimations(R.anim.enter, R.anim.exit);
ft.replace(R.id.fragmentContainer, fragment,fragment.getClass().getName()) // tag
.addToBackStack(fragment.getClass().getName())
.commit();
getSupportFragmentManager().executePendingTransactions();
}
removeFragment()
#Override
public void removeFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().remove(fragment)
.commit();
fragmentManager.popBackStack();
}
You used different name for tags...
you assigned tag by
fragment.getClass().getSimpleName()
And call it by
CartFragment.class.getName()
So just use one of them for both process setting and getting by tag.
P.S. You set tag when you call addToBackStack !
----- Update:
I would suggest to iterate all fragments you have in backstack by:
for (Fragment fragment : getFragmentManager().getFragments()) {
try {
Log.v(TAG,"fragment:"+fragment.getTag()+", isVisible:"+fragment.isVisible());
} catch (NullPointerException e) {
}
}
Then it will be clear what is the tag of fragment to call if exists :-)
use getSupportFragmentManager instead of getFragmentManager when call from activity.
Hee Guys,
I'm currently working with fragments and I'm trying to manage it so that when you click twice on the same menu item it won't put 2 of the same fragments on top of eachother. However it still does. Could anyone tell me what I'm doing wrong?
/*
* Method to check which action is behind the selected Menu item. Then call ShowFragment()
* With the correct fragment parameter used with this Menu action value.
*/
public void getAction(int position, Cursor cursor) {
// TODO Auto-generated method stub
mCursor = cursor;
cursor.moveToPosition(position);
String action = cursor.getString(cursor.getColumnIndex(AppMenu.ACTION));
if (action != null) {
if (action.equalsIgnoreCase("home")) {
trans = manager.beginTransaction();
BaseFragment newFragment = new HomeFragment();
if (manager.findFragmentByTag(newFragment.getTag()) != null) {
mCursor.moveToPosition(position);
// Set the current fragment
mCurrentFragment = newFragment;
Bundle bundle = new Bundle();
int fragId = mCursor.getInt(mCursor.getColumnIndex(AppMenu._ID));
Log.i(TAG, fragId + " ");
bundle.putInt("fragmentID", fragId);
newFragment.setArguments(bundle);
trans.replace(R.id.fragmentContainer, newFragment).addToBackStack(
newFragment.tag());
trans.commit();
} else {
trans.show(newFragment);
}
} else if (action.equalsIgnoreCase("event")) {
showFragment(new EventsFragment(), position);
} else if (action.equalsIgnoreCase("location")) {
showFragment(new LocationsFragment(), position);
} else if (action.equalsIgnoreCase("news")) {
showFragment(new NewsFragment(), position);
} else if (action.equalsIgnoreCase("bars")) {
showFragment(new BarsFragment(), position);
} else if (action.equalsIgnoreCase("currency")) {
showFragment(new CurrencyFragment(), position);
} else if (action.equalsIgnoreCase("map")) {
showFragment(new MapFragment(), position);
}
} else {
Log.i(TAG, "You've got a nullpointerexception on getAction().");
}
}
/*
* Method that's called when changing from fragment through Menu or HomeMenu.
*/
public void showFragment(BaseFragment newFragment, int position) {
trans = manager.beginTransaction();
if (manager.findFragmentByTag(newFragment.tag()) == null) {
mCursor.moveToPosition(position);
// Set the current fragment
mCurrentFragment = newFragment;
// Go on and set the bundle values and pass it on the fragment.
Bundle bundle = new Bundle();
int fragId = mCursor.getInt(mCursor.getColumnIndex(AppMenu._ID));
Log.i(TAG, fragId + " ");
bundle.putInt("fragmentID", fragId);
newFragment.setArguments(bundle);
trans.replace(R.id.fragmentContainer, newFragment).addToBackStack(
newFragment.tag());
trans.commit();
} else {
trans.show(newFragment);
}
}
And here are 2 callbacks for when something changes or when back pressed.
/*
* Interface method called whenever a new fragment is created.
*/
#Override
public void onNewFragment(BaseFragment newFragment) {
// TODO Auto-generated method stub
FragmentManager fm = getSupportFragmentManager();
Class<? extends Fragment> newFragmentClass = newFragment.getClass();
if (newFragmentClass == EventsFragment.class
|| newFragmentClass == LocationsFragment.class
|| newFragmentClass == MapFragment.class
|| newFragmentClass == NewsFragment.class
|| newFragmentClass == CurrencyFragment.class
|| newFragmentClass == BarsFragment.class) {
for (Fragment fragment : fm.getFragments()) {
if (fragment != null && ((Object) fragment).getClass() == newFragmentClass) {
while (((Object) mCurrentFragment).getClass() != newFragmentClass) {
popFragment();
}
popFragment();
break;
}
}
}
}
/*
* Interface method called when you navigate back from a fragment.
* Checks which fragment is active, calls upon this fragments back function to clear any data,
* then pops the first fragment on the backstack.
*/
#Override
public void onBackNavigated() {
// TODO Auto-generated method stub
if ((mCurrentFragment instanceof HomeFragment)
&& !((HomeFragment) mCurrentFragment)
.isStackEmpty()) {
System.exit(0);
// break;
}
if ((mCurrentFragment instanceof LocationsFragment)
&& !((LocationsFragment) mCurrentFragment)
.isStackEmpty()) {
((LocationsFragment) mCurrentFragment).goBack();
// return;
}
if ((mCurrentFragment instanceof EventsFragment)
&& !((EventsFragment) mCurrentFragment)
.isStackEmpty()) {
((EventsFragment) mCurrentFragment).goBack();
// return;
}
if ((mCurrentFragment instanceof MapFragment)
&& !((MapFragment) mCurrentFragment).isStackEmpty()) {
((MapFragment) mCurrentFragment).goBack();
// break;
}
if ((mCurrentFragment instanceof BarsFragment)
&& !((BarsFragment) mCurrentFragment).isStackEmpty()) {
((BarsFragment) mCurrentFragment).goBack();
// break;
}
if ((mCurrentFragment instanceof CurrencyFragment)
&& !((CurrencyFragment) mCurrentFragment).isStackEmpty()) {
((CurrencyFragment) mCurrentFragment).goBack();
// break;
}
popFragment();
}
/*
* Pops the first fragment in the backstack. Then sets this as the new current fragment.
* If no fragment is in the backstack then finish() is called. Which will destroy the only fragment.
* Which in this case exits the application.
*/
public void popFragment() {
if (manager.getBackStackEntryCount() > 1) {
manager.popBackStack();
manager.executePendingTransactions();
ArrayList<Fragment> reversedFragments = new ArrayList<Fragment>(
manager.getFragments());
Collections.reverse(reversedFragments);
for (Fragment fragment : reversedFragments)
if (fragment != null) {
mCurrentFragment = (BaseFragment) fragment;
break;
}
} else {
finish();
}
}
**NOTE : ** The tag() functions calls a final String from the fragment itself with the same hardcoded tag everytime. So every fragment of the same class has the same tag. (Which should prevent double fragments, but it still doesn't)
Solution:
The tag() was returning null al the time. (Don't know why) So I changed the showFragment(fragment, tag, position) and hardcoded the tag in the mainactivity. Then used :
trans.replace(R.id.fragmentContainer, newFragment, tag);
//instead of
trans.replace(R.id.fragmentContainer, newFragment).addToStackBack(tag);
Don't forget to still add it to the backstack! Or else your back navigation won't work.
Just add an extra line: trans.addToBackStack(tag);
You can set the tag while adding/replacing the Fragment,
So you need to mention it as :
trans.replace(R.id.fragmentContainer, newFragment,tag);
Pass the tag value to the method according to the Fragment
showFragment(new EventsFragment(),tag, position);
Hope it will help you ツ
This question already has answers here:
Android: fragments overlapping issue
(17 answers)
Closed 9 years ago.
I have a two pane layout with my buttons on the left. When a button is pressed its corresponding fragment is displayed on the right. Sometimes the fragments overlap but this is an intermittent issue. I can't replicate it all the time but it does happen
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonSettingsManageBooks:
SettingsManageBooksFragment mbFragment = new SettingsManageBooksFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, mbFragment).addToBackStack(null).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsPurchaseHistory:
SettingsPurchaseHistoryFragment phFragment = new SettingsPurchaseHistoryFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, phFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.purchase_history);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsAudio:
SettingsAudioFragment aFragment = new SettingsAudioFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, aFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.audio);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsRestore:
SettingsRestoreFragment rFragment = new SettingsRestoreFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, rFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.restore);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsCopyright:
SettingsCopyrightFragment cFragment = new SettingsCopyrightFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, cFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.copyright);
this.getSupportFragmentManager().executePendingTransactions();
break;
case R.id.imageButtonSettingsAbout:
SettingsAboutFragment abFragment = new SettingsAboutFragment();
getFragmentManager().beginTransaction().replace(R.id.setting_detail_container2, abFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.about);
this.getSupportFragmentManager().executePendingTransactions();
break;
It looks like because I am replacing each fragment with a new fragment that could be causing the overlap. Is there a way to clear all the fragments before committing a new one?
As mentioned by StackOverflowed above this is an intermittent issue. I found a way of fixing this by writing my own code to remove any existing fragments before committing a new one. Below is my code:
fragmentManager = getFragmentManager();
ft = fragmentManager.beginTransaction();
mbFragment = new SettingsManageBooksFragment();
ft.replace(R.id.setting_detail_container2, mbFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonSettingsManageBooks:
if (mPurchaseHistory == true) {
ft.remove(phFragment);
Log.d(TAG, "REMOVING PURCHASE HISTORY FRAG");
} else if (mAudio == true) {
ft.remove(aFragment);
Log.d(TAG, "REMOVING AUDIO FRAG");
} else if (mRestore == true) {
ft.remove(rFragment);
Log.d(TAG, "REMOVING RESTORE FRAG");
} else if (mCopyright == true) {
ft.remove(cFragment);
Log.d(TAG, "REMOVING COPYRIGHT FRAG");
} else if (mAbout == true) {
ft.remove(abFragment);
Log.d(TAG, "REMOVING ABOUT FRAG");
}
ft = fragmentManager.beginTransaction();
mbFragment = new SettingsManageBooksFragment();
ft.replace(R.id.setting_detail_container2, mbFragment).commit();
mImgFragmentTitle.setImageResource(R.drawable.manage_my_books);
mManageBooks = true;
mPurchaseHistory = false;
mAudio = false;
mRestore = false;
mCopyright = false;
mAbout = false;
break;