Andrew
Andrew

Reputation: 1776

How to use both Android.App.Fragment and Support V4 library

I want to use a Support library to add a new L-styled ActionBar to my app. So I changed my activity to an ActionBarActivity. Now I have both getFragmentManager() and getSupportFragmentManager().

I use getFragmentManager() to work with fragments and they are rendered OK. But there is a problem when I add transaction to backstack ((mTransaction.addToBackStack("blabla"))). Backstack remains empty so if I call mTransaction.commit() and right after getFragmentManager().getBackStackEntryCount() latter will return 0 no matter how many transaction I commit. As a result my app will be closed at first "bacK" press without navigation to previous fragment.

Why I don't use Fragments from Support Library? App is targeted to API 15 and newer so I don't care about <11 and there is lot of code to migrate. Also if use Support Library i won't be able to use objectAnimator for transition. It isn't disaster, but I just don't want to degrade one part of app to improve another.

So the question - is it possible to fix back stack or i should migrate all fragment-related code to support-v4?

Upvotes: 0

Views: 638

Answers (2)

Suau
Suau

Reputation: 4738

EDIT: I was posting too fast before reading your question carefully. I've looked through the source code of FragmentActivity and Activity and I highly recommend to use getSupportFragmentManager() if using ActionBarActivity. The reason is that the FragmentManager implementation mFragments is shadowed in FragmentActivity and is used all over the activity life cycle. If using getFragmentManager() the super classes implementation will be accessed. And you will have to rely on FragmentActivity to make the super calls at appropriate times, else the life cycle might get messed up. With onBackPressed() you've already discovered a case where FragmentActivity fails to call super.

Another solution would be to migrate to Toolbar. But you'd loose some of the goodies of ActionBarActivity e.g. widget-tinting and ChildFragmentManager.

I'd just stick with the support implementation.

This is the original answer and doesn't really apply here:

Committing a fragment transaction, does not immediately add/replace your fragment. Therefore your back stack will still be empty. You can call getFragmentManager().executePendingTransactions() if this is really necessary.

Btw: Why would using the support library prevent you from using ObjectAnimator ?

Upvotes: 1

Andrew
Andrew

Reputation: 1776

OK, after few more test I found that actually back stack is filled but it is not handled by activity. Adding following code solved my problem but i'd like to keep this question open since I still don't know why it isn't handled by ActionBarACtivity internally as it is done in Activity

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();

    }
}

Upvotes: 0

Related Questions