Himani
Himani

Reputation: 459

how to avoid blank screen of activity when I back from last fragment

Here I'm using

public void replaceFragment(Fragment fragment, boolean isMainPage, Bundle bundle) {
    String backStateName = fragment.getClass().getName();
    FragmentManager manager = getSupportFragmentManager();
    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

    if (bundle != null) {
        fragment.setArguments(bundle);
    }

    System.out.println("isMainPage :: " + isMainPage);
    System.out.println("FRAGMENT NAME :: " + backStateName + " fragmentPopped :: " + fragmentPopped);

    if (!fragmentPopped) { //fragment not in back stack, create it.
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.main_fragment, fragment);

        if (isMainPage) {
            getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        }
        int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
        System.out.println("backStackCount :: " + backStackCount);
        transaction.addToBackStack(backStateName);
        transaction.commit();
    }
}

for replacing fragments from the entire project. My problem is that when I pop back from the last fragment it shows the blank page where my frame layout placed for replacing fragments

<FrameLayout
    android:id="@+id/main_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Upvotes: 2

Views: 1021

Answers (1)

Avinash Kumar Singh
Avinash Kumar Singh

Reputation: 763

Just remove this line from your code, it will fix your issue.

transaction.addToBackStack(backStateName);

Explanation

You added your fragment in the stack, so that on going back first the fragment is getting pop from the backstack and empty container is visible.

Note: If you want to give tag name for your fragment then just replace your this line of code transaction.replace(R.id.main_fragment, fragment); with this transaction.replace(R.id.main_fragment, fragment, backStateName);

Upvotes: 1

Related Questions