Search code examples
androidandroid-fragmentskotlinandroid-fragmentactivityfragmentmanager

Replacing fragments in FragmentActivity is freezing my application for more than 5 seconds


I have FragmentActivity called StartupActivity. This activity holds 5 Fragments -> LoginFragment, LostPasswordFragment, RegistrationFragment, Login2Fragment, CityPickFragment. I can switch between fragments using ToolBar BackButton. Problem is, that replacing certain fragment using suppportFragmentManager and simple transaction is lagging the app. If I click BackButton it takes nearly 5 second to switch fragment. Fragments has simple layout with 1 ImageView 2 EditTexts and Button (+ ToolBar).

Do you know what can be wrong?

Here is code example how fragment switching works. As I start StartupActivity right after SplashScreen, it will create all 5 Fragments as variables and it will set default Fragment to FrameLayout container.

Then in each 5 fragments is back button in toolbar and I set onClickListener() which calls method in parentActivity to switch fragment.

My Fragments have Tags, because I have to disable onBackPressed() at first fragment(You cant go back to SplashScreen from default Fragment).

//RegistrationFragment
fragmentBackButton.setOnClickListener {
            (activity as StartupActivity).switchToDefaultLoginFragment()
        }

//StartupActivity
fun switchToDefaultLoginFragment(){
        fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.apply {
            replace(R.id.startup_fragment_container, defaultLoginFragment, "LOGIN_FRAGMENT")
            addToBackStack(null)
            commit()
        }
    }

Solution

  • Try to exucute your code inside Runnable . Like this

    Handler  mHandler = new Handler();
    
         Runnable mPendingRunnable = new Runnable() {
                    @Override
                    public void run() {
                        // update the main content by replacing fragments
                        Fragment fragment = getHomeFragment();
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                                android.R.anim.fade_out);
                        fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
                        fragmentTransaction.commitAllowingStateLoss();
                    }
                };
    
                // If mPendingRunnable is not null, then add to the message queue
                if (mPendingRunnable != null) {
                    mHandler.post(mPendingRunnable);
                }
    

    Changing Your Fragment on Main Thread causing that issuie

    EDIT: In kotlin you can use like this

    private var   mDelayHandler : Handler? = Handler()
     mDelayHandler!!.post(mRunnable)
    
    
    internal val mRunnable: Runnable = Runnable {
            Fragment fragment = getHomeFragment();
                            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                                    android.R.anim.fade_out);
                            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
                            fragmentTransaction.commitAllowingStateLoss();
                        }
                    };
        }