2

Possible duplicates with

java.lang.IllegalArgumentException when switching fragments 
But the scenario of the problem is different this Link.

Here is my code

public class MainActivity extends AppCompatActivity{         
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main_layout);
    bindWidgetsWithAnEvent();
 } 
private void bindWidgetsWithAnEvent() {
    tabsLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(final TabLayout.Tab tab) {
            setCurrentTabFragment(tab.getPosition());
            Log.d(TAG, "Position " + tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            Log.d(TAG, String.valueOf(tab.getPosition()));
        }
    });
}
public void setCurrentTabFragment(int tabPosition) {
    switch (tabPosition) {
        case 0:
            replaceFragment(fragmentName);
            break;
        case 1:
            replaceFragment(fragmentName);
            break;
        default:
            break;
    }
}
public void replaceFragment(Fragment fragment) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.flContent, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
}
@Override
protected void onStart() {
    super.onStart();
}
}

The below is the stacktrace caught by Google console, and still didn't get the solution after searching. Please let me know if someone has better solution or have the same problem.

java.lang.IllegalStateException:
  at android.support.v4.app.FragmentManagerImpl.checkStateLoss (FragmentManager.java:1842)
  at android.support.v4.app.FragmentManagerImpl.enqueueAction (FragmentManager.java:1860)
  at android.support.v4.app.BackStackRecord.commitInternal (BackStackRecord.java:650)
  at android.support.v4.app.BackStackRecord.commit (BackStackRecord.java:609)
  at com.safarifone.waafi.ui.activities.MainActivity.replaceFragment (MainActivity.java:354)
  at com.safarifone.waafi.ui.activities.MainActivity.setCurrentTabFragment (MainActivity.java:336)
  at com.safarifone.waafi.ui.activities.MainActivity$3.onTabSelected (MainActivity.java:303)
  at android.support.design.widget.TabLayout.dispatchTabSelected (TabLayout.java:1164)
  at android.support.design.widget.TabLayout.selectTab (TabLayout.java:1157)
  at android.support.design.widget.TabLayout.selectTab (TabLayout.java:1127)
  at android.support.design.widget.TabLayout$Tab.select (TabLayout.java:1426)
  at android.support.design.widget.TabLayout$TabView.performClick (TabLayout.java:1536)
  at android.view.View$PerformClick.run (View.java:20300)
  at android.os.Handler.handleCallback (Handler.java:815)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loop (Looper.java:210)
  at android.app.ActivityThread.main (ActivityThread.java:5833)
  at java.lang.reflect.Method.invoke (Native Method)
  at java.lang.reflect.Method.invoke (Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1113)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:879)

Help will be appreciated

  • try to use ft.commitAllowingStateLoss(); insted of ft.commit(); https://stackoverflow.com/a/18345684/5069323 – Sagar Vasoya Sep 18 '17 at 12:47
  • try to replace ft.replace(R.id.flContent, fragment); with ft.replace(R.id.flContent, fragment,fragment.getClass().getName()); – Pratik Tank Sep 18 '17 at 12:52
  • @PratikTank and SagarVasoya I will check with these options, but are you sure this will be solve the problem. Because I can't reproduce the problem by myself. This is caught by Google Play console. – Muhammad Waseem Sep 18 '17 at 12:58

1 Answers1

2

Basically when you replace fragment it loss its state this is very know bug in Android os and mostly it happen when your activity is in onpause state. I face similar kind issue and I solve it like this by introducing a bollean variable and set it true in catch block. You can reproduce this bug just add post dealy of 10 to 15 sec while commit the fragment and run your activity and put it in onpause state after 10 15 sec you can find crash when it try to commit fragment when activity is in onpause state.

public void replaceFragment(Fragment fragment) {
try {
   FragmentManager fm = getSupportFragmentManager();
   FragmentTransaction ft = fm.beginTransaction();
   ft.replace(R.id.flContent, fragment);
   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
   ft.commit();
  }catch (Exception e)
            {
                stateSaveFragment = true;
                e.printStackTrace();
            }
 }

And in post resume method of activity replace your fragment and set stateSaveFragment to false again. I solve it like this my it help you.

@Override
protected void onPostResume() {
    super.onPostResume();
    if(stateSaveFragment){
        replaceFragment(fragmentName);
    }
    stateSaveFragment=false;
}
Omar Hayat
  • 380
  • 2
  • 12