0
votes

My current Android application has two Fragments:-

ListFragment
Detailfragment

My Layout XML resembles:-

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_anchorGravity="bottom"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
</FrameLayout>

I display the ListFragment first as follows:-

final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId, mListFragment, LIST_FRAGMENT_TAG);

if (mLandscape) {
} else {
       fragmentTransaction.addToBackStack(LIST_FRAGMENT_TAG);
}

fragmentTransaction.commit();

When the user clicks on a List item, I want to hide the List so that I keep the current list position etc.. and display the detailFragment.

Heres the code I use to perform this UI change:-

mDetailFragment = new DetailFragment();

final Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(ITEM_KEY, item.getKey());
mDetailFragment.setArguments(fragmentArguments);

final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if (mLandscape) {
    fragmentTransaction.replace(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
} else {
    fragmentTransaction.hide(mListFragment);
    fragmentTransaction.add(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
}

fragmentTransaction.commit();

The above code all works fine and I can transition between the List and Detail Fragments successfully.

The issue I have is that when the user presses the BACK BUTTON on the detail Fragment to return to the ListFragment they return to a blank screen.

I have this code in my Activity to remove the detail fragment and show the hidden List fragment:-

    @Override
    public void onBackPressed() {

        if  (mLandscape) {
        } else {

        if (mListFragment.isHidden()) {
            final FragmentManager fragmentManager = getFragmentManager();
            final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.remove(mDetailFragment);
            fragmentTransaction.show(mListFragment);
            fragmentTransaction.commit();
        }
    }
    super.onBackPressed();
}

Why is fragmentTransaction.show(mListFragment); not showing the hidden ListFragment?

NOTE: So that I always rebuild my ui completely on orientation changes I have passed a null bundle to super.onCreate(savedInstanceStateNull);

 private final Bundle savedInstanceStateNull = null;
    private boolean mLandscape = false;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceStateNull);
        setContentView(R.layout.activity_article_list);

        mLandscape = getResources().getBoolean(R.bool.landscape);

        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        manageFragments();

    }
1
Ur issue is ListFragment is in the hide postionNithinlal
remove the hide option from the code and try to add the fragment with back stack. I think your issue may be solvedNithinlal
one more thing just remove else part from the on onBackPressed and call popBackStack()Nithinlal

1 Answers

1
votes

Heres how I fixed this issue:-

First remove my overriden onBackPressed()

Change display ListFragment to this:-

final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(containerId, mListFragment, LIST_FRAGMENT_TAG);

fragmentTransaction.commit();

Change display detailFragment to this:-

mDetailFragment = new DetailFragment();

final Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(ITEM_KEY, item.getKey());
mDetailFragment.setArguments(fragmentArguments);

final FragmentManager fragmentManager = getFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if (mLandscape) {
    fragmentTransaction.replace(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
} else {
    fragmentTransaction.hide(mListFragment);
    fragmentTransaction.add(containerId, mDetailFragment, DETAIL_FRAGMENT_TAG);
    fragmentTransaction.addToBackStack(DETAIL_FRAGMENT_TAG);
}

fragmentTransaction.commit();