How can I know the fragment transaction is finished? - android

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.

Related

What new way to refresh a fragment

I used the following method to refresh Fergmant
Fragment frg = null;
frg = getActivity().getSupportFragmentManager().findFragmentById(R.id.fragment_container);
final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Or
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
But after being updated
From
implementation 'androidx.appcompat:appcompat:1.2.0'
To
implementation 'androidx.appcompat:appcompat:1.3.0'
This methods no longer works. What method should I use to refresh the fragment now?
Maybe you can try different usage for fragment refresh:
YourFragment f2 = new YourFragment();
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, f2);
transaction.addToBackStack(null);
transaction.commit();
Yes, the methods you have used are deprecated. You can try this way using the function replace() which replace an existing fragment that was added to a container.
Fragment myFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, myFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Add a fragment and release back stack fragments in one action in Android

How can I add a fragment and release all the back stack in Android under one action?
I've tried:
getSupportFragmentManager().popBackStack(null,FragmentManager.POP_BACK_STACK_INCLUSIVE);
Then to add my new fragment:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.commitAllowingStateLoss();
// or
fragmentTransaction.commit(); // i tried both of them
But I get:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Use ft.addToBackStack(null) to avoid fragment back stack
eg :-
SignUpAsFragment signUpAsFragment=new SignUpAsFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.user_signUp_frag_container,signUpAsFragment); // container is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();

Refresh or rebuild a RowsFragment class

How to refresh or rebuild a class that is extended from RowsFragment?
I have tried a few codes:
RowsFragment frg = this;
frg.getFragmentManager();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
or
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
but still nothing.
You are simply using same instance while reattaching the fragment.
Create new instance of the fragment and replace it with existing one.
RowsFragment fragment = new RowsFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, fragment); // use your container in the place of R.id.container
fragmentTransaction.commit();

non trivial android fragments backstack

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?

Replacing Child Fragment but working only firsttime

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.

Categories

Resources