Create a custom class for change fragment - java

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;
}
}

Related

How to run a function in a created fragment?

I have a Fragment (MapsCoverFragment) which has a FrameLayout in which I launch another Fragment (MapsFragment) with code like this:
// in Class begining
private Fragment mapsFragment;
// in onCreateView()
mapsFragment = new MapsFragment();
getParentFragmentManager()
.beginTransaction()
.replace(R.id.mapsFrame, mapsFragment)
.commit();
In MapsFragment() I have a function:
public void getChargePoints() {...}
That is, I have access to the created MapsFragment through the measured mapsFragment.
How do I run getChargePoints() from MapsCoverFragment?
private Fragment mapsFragment;
That's all I needed to do. I declared the wrong class.

Replace a Fragment with another from a separate java class

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?

How to reload all the fragments at the same time

I have an activity with 2 fragments. Let's call the activity "MainActivity", the first fragment "FragmentA" and the second fragment "FragmentB".
When I delete an element in FragmentA, I use this method inside the FragmentA to realod it:
public void reload_fragment() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
}
But I also need to reload the FragmentB at the same time.
I try to add in the above method that:
public void reload_fragment() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
MainActivity mainActivity = new MainActivity();
mainActivity.reload_fragments();
And then, in my mainActivity, i have this method:
public void reload_fragments(){
viewPager = findViewById(R.id.Viewpager_ID);
adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
}
But it doesn't work.
I tried to add the same method which is inside the FragmentA into the FragmentB, and then call it by the FragmentA, but again, it says "virtual method on NullObjectReference". How can I refresh all the fragments at the same time?
Form a ViewModel with a MutableLiveData boolean field called say "refresh".
Add a method to the ViewModel called "toggleRefresh()" which flips the state of refresh.
In each fragment, in the onAttatch, observe the refresh field and do the "reload there"?

Opening fragment on clicking on Notification

I have one SplashActivity and one MainActivity and all the fragments are associated with MainActivity. Now whenever I recive Notification from server I want to open one specific fragment. I am opening this like
final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if(getIntent().getAction().equals("OpenNotificationFragment")) {
fragmentManager.beginTransaction()
.replace(R.id.fragment_main_container, new NotificationFragment()).addToBackStack(PastOrderFragment.TAG)
.commitAllowingStateLoss();
}
but during my splash image is loading it is throwing NullPointerException, should I call this method in SplashActivity or edit some of my code.

understanding Fragment replacement using FragmentPagerAdapter

I'm having a hard time understanding how to replace fragments using the FragmentPagerAdapter. I have a class that extends FragmentPagerAdapter (android.support.v13.app.FragmentPagerAdapter) and my MainActivity implements ActionBar.TabListener, in the FragmentPageAdapter class I use the getItem() to setup my 3 fragments. so far so good.
One of these fragments is a ListView (pos #1), which I use a Listener to check on the onItemClick().
I want to replace the current ListView Fragment with another fragment so what I did is that inside the onItemClick() I perform the following code:
FragmentManager mFragmentManager = mActivity.getFragmentManager();
//new fragment to replace
Fragment mFragment = new TabPlaceFragment();
mFragmentManager.beginTransaction()
.replace(R.id.pager, mFragment)
.addToBackStack(null)
.commit();
What this is doing is that the fragment is replace with a blank fragment, I'm assuming is the pager, but the mFragment does not get replace. I tried giving an Id to the Current fragment and tried replacing it.
mFragmentManager.beginTransaction()
.replace(R.id.frament_old, mFragment)
.addToBackStack(null)
.commit();
But what this does is that it overlaps the new fragment on top of the old fragment
finally I tried getting the Id of the fragment to replace with the new, but this also gave me a blank fragment.
//this gives me the current fragment I want to replace
Fragment mCurrent = mFragmentManager.findFragmentByTag(MainHelper.getFragmentName(mViewPager.getId(), 1));
mFragmentManager.beginTransaction()
.replace(mCurrent.getId(), mFragment)
.addToBackStack(null)
.commit();
I'm sure I'm not implementing the method correctly but I cannot find a good example to follow the logic of my code. I thought of putting the fragment replacement in the MainActivity under getItem(), but don't know how to call it from within my onItemClick() Listener.
Any help is highly appreciated!!!
Thanks.

Categories