1

I have an activity which implements this method:

public void pushFragment(MyFragment fragment) {
    stack.add(fragment);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.main_frame_layout, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    setTitle(fragment.getTitle(this));
}

In a fragment, I have a listview with this onItemClickListener:

getAppActivity().pushFragment(anotherFragment);

the AnotherFragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    setHasOptionsMenu(true);

    LayoutTransition t = new LayoutTransition();
    container.setLayoutTransition(t);

    View rootView = inflater.inflate(R.layout.another_layout, container, false);

    final ViewGroup vg = (ViewGroup)rootView.findViewById(R.id.avatar_image_layout);
    vg.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);

    // Stuff

    return rootView;
}

On API level >= 19 everything works. On API 18 and 17 (I didn't test on API 16), the first time I click on the listview item it works. Then I go back and click another time on the item. This time the fragment is created, onActivityCreated and onCreateView etc are called, but the fragment isn't displayed.

EDIT 1: Commenting random stuff, I've found out that the problem is:

LayoutTransition t = new LayoutTransition();
container.setLayoutTransition(t);

commenting these two lines solves the problem, but I don't know why and I loose the animation...

andrew
  • 3,879
  • 4
  • 25
  • 43
  • what is your code when pressing the backpress? did you override it and use fragment.popbackstack()? – Rashid Apr 20 '16 at 07:39
  • Yes, but as you can see from my EDIT 1, the problem is elsewhere – andrew Apr 20 '16 at 08:21
  • Im avoiding using animation on my fragment but if you want to, I think this might help you http://stackoverflow.com/questions/9194311/pop-the-fragment-backstack-without-playing-the-pop-animation – Rashid Apr 20 '16 at 08:24

1 Answers1

0

This may vary among APIs, but it is possible that FragmentTransaction isn't applied instantly after calling commit() (info).

According to FragmentTransaction.java, the commit() method states that:

 * Schedules a commit of this transaction.  The commit does
 * not happen immediately; it will be scheduled as work on the main thread
 * to be done the next time that thread is ready.

In order to make this happening immediately, after the commit() method, we call FragmentManager.java's executePendingTransactions() (more info):

 * After a {@link FragmentTransaction} is committed with
 * {@link FragmentTransaction#commit FragmentTransaction.commit()}, it
 * is scheduled to be executed asynchronously on the process's main thread.
 * If you want to immediately executing any such pending operations, you
 * can call this function (only from the main thread) to do so.  Note that
 * all callbacks and other related behavior will be done from within this
 * call, so be careful about where this is called from.
Community
  • 1
  • 1
ReVs
  • 57
  • 5