What new way to refresh a fragment - android

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();

Related

Declared target fragment that does not belong to this FragmentManager

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.

Fragment inside a DialogFragment, blank screen

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">

Refresh a fragment android

Earlier this code was working but it now it suddenly stopped working. Fragment is not detaching from parent activity.
public void reLoadFragment(Fragment fragment)
{
Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment");
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag(fragment.getClass().getName());
frg.onDetach();
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment finish");
}
ft.detach() not working after support library update 25.1.0. This solution works fine after update:
getSupportFragmentManager().beginTransaction().detach(oldFragment).commitNowAllowingStateLoss();
getSupportFragmentManager().beginTransaction().attach(oldFragment).commitAllowingStateLoss();
Credit:
Refresh or force redraw the fragment
You can simply refresh the current fragment using
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Also, in your case you can do something like this,
public void reLoadFragment(Fragment fragment)
{
Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment");
Fragment currentFragment = fragment;
if (currentFragment instanceof "FRAGMENT CLASS NAME") {
FragmentTransaction fragTransaction = (getActivity()).getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();
Log.i(LogGeneratorHelper.INFO_TAG, "reloading fragment finish");
}
else Log.i(LogGeneratorHelper.INFO_TAG, "fragment reloading failed");
}
Use this in yout Main Activity:
frag_name frag_name = new frag_name();
FragmentManager manager = this.getSupportFragmentManager();
manager.beginTransaction().replace(R.id.youtframelayout, frag_name, frag_name.getTag()).commit();
It inicializes the fragment and replaces it in your FrameLayout
Like this your replacing again the fragment :)
Hope it works! :)
This will refresh current fragment :
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
For kotlin use below code.
fragmentManager!!.beginTransaction().detach(this).attach(this).commit()
For kotlin:
supportFragmentManager.beginTransaction().detach(fragment).attach(fragment).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();

How can I know the fragment transaction is finished?

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.

Categories

Resources