In Fragment3 have child fragment, if i open Fragment3 from Fragment1 then Fragment4 is shown but now i open fragment3 from fragment2 then fragment 4 is not shown.
And vice-versa if i open Fragment 3 from Fragment 2 then Fragment4 is shown, but if i open Fragment 3 from Fragment1 then Fragment 4 is not shown.
Code to add Fragment 4:
Fixture_H2h_frag fragment1=new Fixture_H2h_frag();
Bundle b1 = new Bundle();
b1.putBoolean("Show", true);
//b.putString("RESULT", result);
b1.putString("URL", Url);
b1.putString("TYPE", "FORM");
b1.putString("VisitorTeam1", matchlist.get(pos).getVisitor_image_url());
b1.putString("LocalTeam1", matchlist.get(pos).getLocal_image_url());
fragment1.setArguments(b1);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.Inner_Fixture_Container, fragment1);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Code to add Parent Fragment:
FixtureDescrption_Frag frag = new FixtureDescrption_Frag();
FragmentManager fm = contxt.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.fragment_place, frag);
fragmentTransaction
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
if(addedtostack){
fragmentTransaction.addToBackStack(contxt.getClass().getName());
}
fragmentTransaction.commit();
You need to use
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
instead of
FragmentTransaction transaction = getFragmentManager().beginTransaction();
While adding child fragment.
You can refer this link for more details.
Related
I have a dialog fragment(FragmentA), with a frame layout.
I am trying to add another fragment(FragmentB) into the frame layout.
public void setFragment(Fragment fragment) {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.frame_canvas, fragment);
transaction.commit();
}
Just to confirm, I am logging inside OnCreateView of FragmentB, and it gets printed when call setFragment.
But the view of FragmentA doesn't add FragmentB into the frame layout.
Edit:
FragmentA is actually a DialogFragment. Can that be the issue?
Use this I think it will works.
If you on Fragment A than use code like below
Fragment fr = new FragmentB();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, fr);
fragmentTransaction.commit();
if you are on activity from where you call fragment B than use Code like below `
Fragment fr = new FragmentB();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.frame_layout, fr);
ft.commit();`
// xml
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/general_home_toolbar">
I'm trying to implement the following fragment design:
Fragment A is replaced by fragment B, which in turn, is replaced by fragment C.
Whether in fragment B or C, I want the user back navigation to take it back to fragment A.
I add to backstack when replacing A with B. But when I move from B to C, I don't add to backstack.
When navigating back from fragment B, everything works fine.
But, when navigating back from C, I get A and C on the same screen - C doesn't disappear.
I wonder if it is related to my backstack usage.
Any help is appreciated.
My code is equivalent to:
Fragment fragment;
fragment = new FragmentA();
transaction.replace(R.id.container, fragment);
transaction.commit();
fragment = new FragmentB();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
fragment = new FragmentC();
transaction.replace(R.id.container, fragment);
transaction.commit();
This is the general way to add to Backstack. Use tags.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragmentTransaction.replace(R.id.fragment_container, fragment1, Fragment1.class.getName());
fragmentTransaction.addToBackStack(Fragment1.class.getName());
fragmentTransaction.commit();
Now similarly, for frament2:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragmentTransaction.replace(R.id.fragment_container, fragment2, Fragment2.class.getName());
fragmentTransaction.addToBackStack(Fragment2.class.getName());
fragmentTransaction.commit();
Do the same for fragment3.
And now you remove fragment2 from backstack using:
getFragmentManager().popBackStack(
Fragment2.class.getName(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);
You should be able to skip directly from fragment3 back to fragment2.
Also, are you using fragmentTransaction.replace(...) and not fragmentTransaction.add(...). Could you post relevant code?
I have 4 Fragments. A B C D
I have kept only fragment A in backstack.
I go from fragment A -> B -> C -> D
now on backpress in fragment D, I should redirect to fragment A but rite now both A and D fragment gets displayed on screen together.
How do I hide fragment D?
What if you popbackstack ..adding fragment A and launch any fragment..
for example:
Fragment fragmentA = new FragmentA();
Fragment fragmentD = new FragmentD();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
manager.popBackStack();
transaction.addToBackStack(fragmentA.getClass().getName());
transaction.replace(R.id.frame_container, fragmentD);
transaction.commit();
Step 1 : Write code in Main Activity who contains Frame Layout it's name is fragcontainer
getSupportFragmentManager().beginTransaction().add(R.id.frgContainer,new FragOne(),FragOne.class.getName()).commit();
Write Code on "FragOne"
FragThird FragThird = new FragThird();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
// ft.addToBackStack(FragTrackView.class.getName());
ft.add(R.id.frgContainer, FragThird, FragFourth.class.getName());
ft.commit();
same it is add 2 Fragment and Last Fragment add like this
FragFourth FragFourth= new FragFourth();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left, R.anim.slide_in_right, R.anim.slide_out_right);
ft.addToBackStack(FragFourth.class.getName());
ft.add(R.id.frgContainer, FragFourth, FragFourth.class.getName());
ft.commit();
So lets say I have the following Fragments:
Fragment1
Fragment2
DialogFragment
Fragment3
Fragment4
Lets say I go to the following Fragments, each fragmetn is added to the back stack:
Fragment1 -> Fragment2 -> DialogFragment -> Fragment3 -> Fragment4
If I click the BACK button this should normally go to Fragment3 but instead DialogFragment is displayed.
Here are the Fragment Transactions I used:
Fragment1 -> Fragment2
FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainContentHolder, new Fragment2(), "Fragment2");
ft.addToBackStack(null);
ft.commit();
Fragment2 -> DialogFragment
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.setCustomAnimations(R.anim.flipp_in,R.anim.flipp_static);
ft2.add(R.id.mainContentHolder, new DialogFragment(), "DialogFragment");
ft2.addToBackStack(null);
ft2.commit();
ft2.hide(Fragment1.this);
DialogFragment -> Fragment3
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainContentHolder, new Fragment3(), "Fragment3");
ft.addToBackStack(null);
ft.commit();
Fragment3 -> Fragment4
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainContentHolder, new Fragment4(), "Fragment4");
ft.addToBackStack(null);
ft.commit();
Again the problem is that when I'm in Fragment4 and click the BACK button the DialogFragment is displayed instead of Fragment3.
Please use fragment manager of your Activity in Fragments when showing DialogFragment to avoid restoring it from back stack.
I want to make two fragment transactions in a row.
Fragment1 fragment1 = new Fragment1();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment1);
transaction.addToBackStack(null);
transaction.commit();
Fragment2 fragment2 = new Fragment2();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment2);
transaction.addToBackStack(null);
transaction.commit();
However, if I do so, there will be some concurrence problem.
Is there a way for me to implement a callback function so that I can start the second transaction right after the first one is finished?
You can manually flush the queue causing the pending fragment transaction to be executed. The call you're looking for is:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(android.R.id.content, fragment, name).addToBackStack(null).commit();
fm.executePendingTransactions();
By calling the fm.executePendingTransactions(); you force all pending actions to occur before continuing.