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.
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 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"?
I'm struggling with the changing of a fragment from another fragment. In my main_activity i have a layout-file with a bottomnavigationview and a framelayout for the content on the page. The changing from fragment to fragment on the navigationview workes fine, but if i access a new "subfragment" from a fragment, the new fragment is transparent.
Any suggestions? code below from my fragment and my mainactivity.
MainActivity parentActivity = (MainActivity) getActivity();
parentActivity.switchContent(new VisArrangementInfo(), data, true);
switchContent inside MainActivity:
public void switchContent(final Fragment fragment, Bundle data, final Boolean addToBackStackOption){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
}
The id content_frame is the id for the layoutfile for my MainActivity. the bottomnavigationview and the framelayout is in this file. the ID for the framelayout is "main_frame".
After my understanding i would want to change the "main_frame" and not the "content_fram" since it's here the main-content is supposed to be. When i do that i can't find the layout for the fragment i try to open at all. Is it because i'm trying to replace the framelayout instead of the whole layout?
In your Fragment A define an interface.
public interface FragmentEventListener {
void onFragmentChangeEvent(Fragment newFragment);
}
private FragmentEventListener listener;
public void setEventListener(FragmentEventListener listener) {
this.listener = listener;
}
In your fragment, when you want to change the fragment, call the method.
if (listener != null) {
listener.onFragmentChangeEvent(FragmentB);
}
In your main activity, implement FragmentEventListener, so you can switch to Fragment B from MainActivity.
public class MainActivity implements FragmentA.FragmentEventListener {
...
...
fragmentB.setEventListener(this); // here identify the listener.
....
#Override
public void onFragmentChangeEvent(Fragment newFragment) {
switchContent // Switch to Fragment B here
}
In the first time use add instead of replace:
public void switchContent(final Fragment fragment, Bundle data, final Boolean addToBackStackOption){
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
}
So, what my problem is that in one fragment(w/i a viewpager, I'll call this Fragment A) I click on this dynamically created button that adds a new fragment(I'll call this Fragment B) in a framelayout which allows me to use PayPal service. On PayPal Activity result, Fragment B communicates with the main Activity via a communicator(an interface class) to call Fragment A to change that text. But I'm getting a null pointer exeception crash.
To be specific:
what I did was that I made a global TextView variable that is initialized on click. I did this b/c I have a list of other things that are dynamically inflated and to avoid the TextView from being initialized with wrong layout I initialized it on click.
bidChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
eventListChangeKey = keyVal;
eventListChangeIdx = eventListIdx;
eventBiddingChangeIdx = finalI;
priceToChage = (TextView) biddersLayout.findViewById(R.id.single_list_bidder_bid_price);
Bundle bundle = new Bundle();
bundle.putInt("auctionID", auctionId);
bundle.putInt("dateID", dateId);
bundle.putInt("FromWhere", 2);
Fragment fragment = new Fragment_Home_ItemInfo_Bid();
fragment.setArguments(bundle);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.container_mainScreen, fragment, "itemInfo_bid")
.addToBackStack(null)
.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}
});
In the main activity
public void changeBidderPrice(String s) {
Fragment fragment = viewPagerAdapter.getItem(1);
((Fragment_List) fragment).changePrice(s);
}
is what I do
back in Fragment A
public void changePrice(String val) {
priceToChage.setText(val);
dataMap.get(eventListChangeKey).get(eventListChangeIdx).getBidList().get(eventBiddingChangeIdx).setPrice(val);
}
I've thought this over an over but I just can't figure this out. I've searched for similar cases in StackOverflow but I wasn't able to get a help.
Would the problem be the way I initialize that TextView? or is it the way I'm calling Fragment A from the main activity?
for fragments onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null. Any view setup should happen here. E.g., view lookups, attaching listeners.
source : codepath
for activities onCreate()
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.