Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Method from FragmentTransaction doesn't remove fragment from Container

I have the following problem:

My application needs to perform navigation on a tablet as follows:

+----+-------+    +----+-------+    +----+-------+    +----+-------+
|    |       |    |    |       |    |    |       |    |    |       | 
|  1 |   2   | -> |  2 |  3    | -> | 3  |  4    | -> | 4  |  ...  |
|    |       |    |    |       |    |    |       |    |    |       |
+----+-------+    +----+-------+    +----+-------+    +----+-------+

I tried to make this navigation using FRAGMENTS, since the navigation is dynamic and I do not know how many levels this navigation will have.

Using the API fragments, I created the following structure to assist in the tests, calling the same fragment multiple times:

If the Left Panel is empty, insert the fragment in it. If the Left Panel is not empty and the Right pane is empty, insert the fragment on the Right pane. If the two Panels are not empty, remove the fragment of the Right Panel and substitute the Left panel and for it. Then insert the new fragment to the right.

The problem is that I found the error "Can not change ID of container fragment" when I try to perform the replace method.

Looking at the documentation for Remove method of the transaction have the following information: "Remove an existing fragment. If it was added to a container, its view is also removed from that container."

But looking in the API code fragment, there is still a variable that contains the reference to the former Container: mFragmentId.

Does anyone know a way to accomplish this navigation or reset this variable?

like image 747
Helton Isac Avatar asked Nov 04 '22 03:11

Helton Isac


1 Answers

I think I found a solution to the problem:

Instead of two layouts, I'll just use one.

The orientation of this layout is horizontal, then the new Fragments will be added in the left side always.

however, using the following code:

Frag1 frag1 = new Frag1();
frag1.setText("Fragment " + Integer.toString(navigation));
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (navigation > 2) {
    ft.remove(fm.findFragmentByTag("f" + Integer.toString(navigation - 2)));
}
ft.add(R.id.Panes, frag1, "f" + Integer.toString(navigation));
if (navigation > 1) {
    ft.addToBackStack(null);
}
ft.commit();

I have a problem in the BackStack, because the stack will insert the removed Fragment and remove the last inserted one, reversing the order.

To fix this, I changed the transaction remove to hide, But I still don't know if this will give me performance problems.

ft.hide(fm.findFragmentByTag("f" + Integer.toString(navigation - 2)));

It works for me.

like image 107
Helton Isac Avatar answered Nov 09 '22 16:11

Helton Isac