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();
Related
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();
I've got a simple setup:
Code:
ActivityHaupt -> FragmentHaupt:
final FragmentHaupt fragment = new FragmentHaupt();
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frameLHaupt, fragment, null);
transaction.disallowAddToBackStack();
transaction.commit();
FragmentHaupt -> FragmentViewP:
FragmentViewPfragment2 = new FragmentViewP();
fragment2.setTargetFragment(FragmentHaupt.this, 12); //AppConstant.FRAGMENT_CODE = 12
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLHaupt, fragment2);
fragmentTransaction.commit();
FragmentViewP-> FragmentAddDriver:
FragmentAddDriver ldf = new FragmentAddDriver ();
Bundle args = new Bundle();
args.putString("firstname", vName.getText().toString());
args.putString("lastname", nName.getText().toString());
args.putString("drivername", dName.getText().toString());
ldf.setArguments(args);
ldf.setTargetFragment(FragmentHaupt.newInstance("firstP","lastP"), 12);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLHaupt, ldf);
fragmentTransaction.commit();
When I try opening the FragmentAddDriver activity, I receive the error in the title. What can the problem be? I am using always the same FragmentManager (getActivity().getSupportFragmentManager)?
What am I missing?
With the code here:
ldf.setTargetFragment(FragmentHaupt.newInstance("firstP","lastP"), 12);
You're not actually going back to the FragmentHaupt. You're adding a new instance of it.
To actually go back you've to set tag when you add it in MainActivity
transaction.add(R.id.frameLHaupt, fragment, "FragmentHaupt");
And inside FragmentViewP you've to find it using that tag.
ldf.setTargetFragment(fragmentManager.findFragmentByTag("FragmentHaupt"), 12);
This way you'll be able to go back to that instance.
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 use the one frame on which changing the fragment views, the problem I have is when I place add instead of replace the new view is placed on the existing one
all I need is it has to replace the existing and place the new one
As I have to reuse the current view when back button is pressed cannot use the replace
Fragment fragment = null;
fragment = new DetailFragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container2, fragment)
.addToBackStack(null).commit();
If you don't use the support library (which is your actual case), use this code:
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
If one day you'll decide to use the support library, then use this instead (it's only a matter of replacing getFragmentManager() with getSupportFragmentManager()):
final FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction;
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container2,fragment.newIstance());
transaction.addToBackStack(null);
transaction.commit();
And you need to create newIstance in Fragment class:
public static fragment newIstance(){
fragment frag= new fragment();
return frag;
}
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.