I have a Recycler View in a Fragment that I have an adapter class for in a separate file. I am trying to make it so that when you click on a RecyclerView Item it replaces the current fragment with a new one. The code to replace a fragment with another that I had been using previously is this:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this works inside the fragment, but since I have to call it from the java class I tried this instead:
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this just layers the new fragment on top of the current one. If I try making a class in the Current Fragment with the transaction code in it and call it, it doesn't work because of the static context.
Is there any way to do this?
Related
I am trying to create a custom class to handle change fragment. However, I cannot get the fragment manager in order to execute change fragment. What I have tried is to extend the class to FragmentActivity and get the fragment manager by using getSupportFragmentManager(). However, it returns error with Fragment manager not attached to host
Any idea that I can get the correct fragment manager to execute changing fragment? should I extend to another class?
public class changefragment extends FragmentActivity{
public void ChangeFragment(){
Fragment f = new MyCustomFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.nav_host_fragment,f);
ft.addToBackStack(null);
ft.commit;
}
}
i have below code for data remove from DB.
So i delete data but i want to update fragment from here.
how i can refesh fragment ?
// Remove entry from database table
database.delete(FAVORITES_TABLE, FAVORITES_ITEM_ID + " = " + soundObject.getItemID(), null);
// Refesh current fragment
????????????????????????????????
this is code which i use in Fragment to refesh it (on button press) work good, but i don't want to tap refesh button to refesh page. I want fragment refesh when DB change by it self.
public void refersh()
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
}
any idea?
Thanks
you are detaching and attaching again in one commit, so it does nothing. make it two commits
public void refersh(){
FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft1.detach(this).commit();
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.attach(this).commit();
}
there is also possibility that you shoud use getSupportFragmentManager instead of getFragmentManager
its probably not best/most-efficient solution, but you posted ony this small snippet...
In simple terms you can use:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
I think you want to refresh the fragment contents upon DB data updates
If so, detach the fragment and again attach it
// Reload current fragment
Fragment f1 = null;
f1 = getSupportFragmentManager().findFragmentByTag("Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(f1);
ft.attach(frg);
ft.commit();
Fragment_TAG is the name you gave your fragment when you created it
To refresh the current fragment content, you can use the following code inside the method refresh()
public void refresh()
{
Fragment fragment = null;
fragment = new Current_Fragment_Name(); //replace the Current_Fragment_Name with your current fragment name
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
Hope it works for you.
I have an app with functionality to show one record or a list of them. I have a fragment for single record, so I decided for a list I want to fill a RecyclerView with cards, and add a fragment into each card. I am getting IllegalArgumentException: Cannot add a null child to a ViewGroup on a line in onBindViewHolder():
holder.frameLayout.addView(ResultFragment.newInstance(id).getView())
I'm not so sure how to work with a Recycled View and Fragments but if you use a linearLayout or RelativeLayout, you can use FragmentManager in the onCreateView method or generate you're pwn method to change the content in the view (fragment) that you want to change:
FragmentManager fm = getSupportFragmentManager();
transaction = fm.beginTransaction();
oneFragment = new OneFragment();
transaction.replace(R.id.fragmentContent, oneFragment);
transaction.commit();
the R.id.fragmentManager are a <fragment> section in your view
Regards
I have Fragment A.
On clicking some button, i add Fragment B above Fragment A.
FragmentB fragmentB= new FragmentB ();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_home, fragmentB, "fragmentB");
fragmentTransaction.hide(FragmentA.this);
fragmentTransaction.addToBackStack(FragmentA.class.getName());
fragmentTransaction.commit();
I perform some action in Fragment B. After finishing my work i popBackStack() current fragment.
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack();
Now i Fragment A. Since i used "ADD fragment" all my values are kept in edittext.
I want to know, which method is invoked when i come back to Fragment A,
is it onResume()..?
I have to put log in onResume, seems like it not going to onResume!
instead of fragmentTransaction.add(R.id.content_home, fragmentB, "fragmentB");
use
fragmentTransaction.replace(R.id.content_home, fragmentB, "fragmentB");
refer link Difference between add(), replace(), and addToBackStack()
try below code
FragmentB fragmentB= new FragmentB ();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_home, fragmentB, "fragmentB");
fragmentTransaction.hide(FragmentA.this);
fragmentTransaction.addToBackStack(FragmentA.class.getName());
fragmentTransaction.commit();
According to Fragment documentation, your fragment will be resumed after pressing back.
https://developer.android.com/guide/components/fragments.html
if you do call addToBackStack() when removing a fragment,
then the fragment is stopped and will be resumed if the user navigates
back.
But you said,
I have to put log in onResume, seems like it not going to onResume!
Then you may have some problems in your code:
- If you do
fragmentTransaction.hide(FragmentA.this);
then you may need to use FragmentTransaction.show(Fragment), in order to display FragmentA again. And then it will go to onResume().
I'm using the following code to create a fragment everytime the user click on an item in a list view.
But in this way the fragment is created at every user click. What I want is to reuse the old fragment (if it exists) and only reload its content (don't create a new one).
MagazineViewFragment fragment = new MagazineViewFragment();
fragment.openStream(itemSelected);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
How can I do?
There're multiple ways, probably the most easy one is to check if the current Fragment in your container is an instance of FragmentXYZ (in your case MagazineViewFragment).
Example
Fragment mFragment = getFragmentManager().findFragmentById(R.id.container);
if (mFragment instanceof MagazineViewFragment)
return;
Add tag when you call your fragment from activity:
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentByTag( MagazineViewFragment.TAG);
if (fragment == null) {
MagazineViewFragment fragment = new MagazineViewFragment();
fragment.openStream(itemSelected);
getFragmentManager()
.beginTransaction()
.add(R.id.container, fragment, MagazineViewFragment.TAG)
.commit();
}
If you need only to update itemSelected - see broadcasts or listeners.
Something like this might help:
getFragmentManager().findFragmentById(fragmentId);
Do not forget the null check.