SafeFragmentTransaction

Additional

Language
Java
Version
v1.1.2 (Feb 28, 2018)
Created
Nov 15, 2017
Updated
Feb 28, 2018
Owner
George Dzotsenidze (Zuluft)
Contributor
George Dzotsenidze (Zuluft)
1
Activity
Badge
Generate
Download
Source code
APK file

Commercial

SafeFragmentTransaction

I'm sure that every android developer has faced exception

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

after fragmentTransaction.commit() or fragmentManager.popBackStack(...) when activity is paused. To avoid this, use SafeFragmentTransaction. If activity is paused and fragment transactions are requested, SafeFragmentTransaction ensures that these transactions will be executed after activity comes alive. So there are no crashes and life is beautiful. It's very very easy to use:

Gradle:

Add it in your root build.gradle at the end of repositories:

 allprojects {
  repositories {
   ...
   maven { url 'https://jitpack.io' }
  }
 }

Add dependency to app gradle:

implementation 'com.github.Zuluft:SafeFragmentTransaction:v1.0'

Step 1:

Simply extend your activity by SafeFragmentTransactorActivity

or

Write the following in your base activity:

...
private SafeFragmentTransaction mSafeFragmentTransaction;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSafeFragmentTransaction = SafeFragmentTransaction.createInstance(getLifecycle(),
                getSupportFragmentManager());
        getLifecycle().addObserver(mSafeFragmentTransaction);
    }

    public final SafeFragmentTransaction getSafeFragmentTransaction() {
        return mSafeFragmentTransaction;
    }
...

Step 2:

When adding, replacing or poping fragments, write the following:

...
getSafeFragmentTransaction().registerFragmentTransition(fragmentManager ->
                    fragmentManager.popBackStackImmediate()
                    fragmentManager.beginTransaction()
                            .add(R.id.content1, new Fragment1(), "TAG1")
                            .replace(R.id.content2, new Fragment2(), "TAG2")
                            .commit());
...

You can use SafeFragmentTransaction for child fragments too. Write the following in onCreateView(...) of your fragment:

mSafeFragmentTransaction = SafeFragmentTransaction.createInstance(getLifecycle(),
               getChildFragmentManager());
getLifecycle().addObserver(mSafeFragmentTransaction);

And use it as it is shown in step 2.