Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Back key on programmatically added Fragment leads to empty container

I have a problem with a fragmented layout and I sincerely apologize if it has been answered before and I was too dumb to find it. I searched for hours and got nothing (well, I got lots but nothing solved my problem).

So here's my setup: I have a two pane layout using two FrameLayouts as containers for my fragments. activity_listing.xml:

<FrameLayout android:id="@+id/listing" />
<FrameLayout android:id="@+id/details" />

On opening the app (onCreate in the fragment's activity) a fragment called Listing is added to FrameLayout "listing" programmatically using a FragmentTransaction.

public class ListingActivity extends FragmentActivity
        implements ListingFragment.Callbacks {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listing);

    // Displaying the first listing here ...
    Fragment fragment = new ListingFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.listing, fragment);
    ft.addToBackStack(null);
    ft.commit();
    ...
}

This ListingFragment is replaced a few times during runtime, so I have to create it instead of defining it in XML. I tried the latter with

<fragment android:name="com.example.app.ListingFragment" />

but then it won't be replaced or removed programmatically later on. I can only add() and then the old one is visible through the new one. But that's not the problem here - just an explanation for my way.

So far all of this works as it should, Listing is created and displayed etc., so no problems there. But:

When ListingFragment is displayed at startup and I press the back key, at the last position FrameLayout "listing" is emptied instead of dropping back to the Homescreen! I figured it has to be because onCreate of my ListingActivity I display an empty frame and add() a ListingFragment to it. So the back stack has the empty frame in it, too. Right?

I tried solving the situation by this to ListingActivity:

@Override
public void onBackPressed() {
    super.onBackPressed();
    if(getSupportFragmentManager().getBackStackEntryCount() == 0) {
        this.finish();
    }
}

But somehow that does not look or feel right ... it looks like a bad work around.

So is there any way to insert the fragment before the view with the empty FrameLayout is inflated so there is no empty state to "back" to? Or is it possible to remove the "empty" state from the back stack even though it is not in it? Any other ideas on how to avoid the empty frame after hitting "back"?

Thank you very much for your efforts in advance!

like image 974
Bobby Tables Avatar asked Oct 16 '12 16:10

Bobby Tables


1 Answers

Don't call ft.addToBackStack(null) when you add the Fragment in onCreate. That tells the FragmentManger that you have another state BEFORE that fragment that you want to be able to jump back to.

like image 74
toadzky Avatar answered Nov 04 '22 13:11

toadzky