I got a fragment A containing a child fragment B. How is the correct way to create and add the child fragment B in fragment A so that any android-magic works?
Currently I initialise child fragment B in constructor of fragment A and add it in onCreateView of fragment A (via FragmentTransaction.replace)
This works fine for the first time. But if the fragment is paused/resumed fragment A constructor is called, creating child fragment B and additionally android creates child fragment B automatically. So there are 2 child fragment B... I think it would be best to somehow use child fragment B created from android.
You can check whether the Fragment B exists
Fragment b = fragmentManager.findFragmentById(R.id.fragment_b_container);
if(b == null) {
//initialize the fragment transaction here...
}
Related
I have an activity, with multiple master-detail fragments in the activity.
Fragment 1 is opened with a button click in Activity, and Fragment 2 is open with a button click in Fragment 1.
Activity => Fragment 1 => Fragment 2
As I know, onWindowFocusChanged() is called when Activity switch to Fragment 1.
However, it is not called when Fragment 1 switch to Fragment 2.
Is there any callback method invoked when switching Fragment 1 to Fragment 2 that I can use?
I need this call back so that I can render my Fragment 2 layout correctly as I added some TableRow with TextView programmatically in Fragment 2.
I tried using onCreateView(), onStart(), onCreate() methods in Fragment 2 but it doesn't work because the layout(TextView, TableRow) that I added programmatically in Fragment 2 isn't added to the RootView yet.
Any suggestion or solution?
I have 3 fragments Fragment A is the main fragment and Fragment B is a child fragment. When clicking on fragment B , I want fragment C to replace the whole fragment A and navigate to it again when button back is pressed from C.
The problem that i can't solve is that the framelayout container is in Fragment A xml so what fragment container i should give to fragment C :
Fragment fragment = new PetDetailFragment();
String fragmentTag = fragment.getClass().getName();
getFragmentManager().beginTransaction().replace(R.id.frameLayout, fragment, fragmentTag).commit();
when the user clicks on fragment B just execute
getFragmentManager().beginTransaction().add(R.id.frameLayout, new FragmentC(), fragmentCTag).commit();
and then when the user presses back fragment A will be visible automatically. To display fragment A manually execute
getfragmentmanager().popbackstack()
I have an Activity. It adds Fragment A dynamically. Fragment A contains static added (by xml) child Fragment B. How can I pass data from Activity to Fragment B when i add Fragment A to Activity?
I know next approach:
Implement interface getter in Activity and call getter method from Fragment B
I'd like to know, are there another ways to do it?
I tried set arguments from Activity to Fragment A, find Fragment B by child fragment manager and set arguments from Fragment A to Fragment B.
But Fragment B created earlier than Fragment A, so i had exceptions..
Well there are more than one ways to accomplish that. one of the way is
Create a function public void setData(Data data) in your Fragment B
Transfer your data from Activity to Fragment A. Since your Fragment B is added by fragment tag in xml. create ref to your Fragment B object in Fragment A. Now you have the reference to Fragment B in A.
Now Simply call the function (created in Fragment B) in Fragment A and pass the data which you have received by the Activity
Other way..
In Fragment B you can call getActivity() and type cast it in to your Activity class type and call its method which can return the desired data
This sample by google should resolve all your Issues:
https://developer.android.com/training/basics/fragments/communicating.html
You can try EventBus to pass data from Activity to your Fragment B. In your activity call this EventBus.getDefault().post(new MessageEvent());
And then you just need to register EventBus in onCreateView of Fragment B
EventBus.getDefault().register(this);
And then
#Subscribe
public void onEvent(MessageEvent event){
// DO SOMETHING
}
I have a FragmentActivity in which I am displaying two Fragments, Fragment A and Fragment B. In Fragment A I have another Fragment with Tabs. I have done this using classic TabHost. In this TabHost I have 2 more Fragments which has a ListView. Now what I want is on clicking on listitem I want to replace the content in Fragment B of parent FragmentActivity. Please help me to achieve this. I have tried the following till now.
1. View mContainer = (View)getActivity().findViewById(R.id.rightpane);
2. ViewGroup mContainer = (ViewGroup)getView().getParent();
3. Activity rrlist = ((RRPatientList)getParentFragment()).getActivity().getParent();
View mContainer = (View) rrlist.findViewById(R.id.rightpane);
ResultDetailView rdl = new ResultDetailView();
rdl.setArguments(args);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(mContainer.getId(), rdl);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
Nothing is working and I am getting either NullPointerException or No View Found for the ID ****** errors.
Please refer the screen shot for more info.
create method(like: updateFragment) in your activity to update fragment B. from fragment A's child fragment call that method(updateFragment) then that method(updateFragment) update fragment B.
here is answer for update fragment from activity - it 'll help you to update fragment B from your parent activity
here is answer for call activity method from fragment - it 'll help you to call parent activity method from fragment A's child fragment
I'm new to fragments. I have an activity that extneds FragmentActivity and uses the ViewPager to swipe between fragments.
Within one of the fragments I want to launch a new fragment that is "outside" of whats in the view pager. Is it possible to add a fragment like this or do I need to start a new activity with the fragment.
Now I have it launch a new activity but it is very slow.
Activity A
Fragment A
Fragment B
Fragment C
Activity B
Fragment D
Fragment A
So fragment A can launch acitvity B to get to fragment D but ideally it would be cool if I could just inject fragment D in Activity A
Hope that makes sense.
Yes, you can, but I strongly suggest you avoid it, unless you have a good reason to do so.
The ViewPager is (I assume) hosted in your Activity. The ViewPager obtains its views from its adapter. (A FragmentStateAdapter or similar). So you could tell the activity to change its layout (and or hide the Viewpager and show another FrameLayout) or you can simply launch another Activity that contains a single fragment. This is also fine.
Messing with the ViewPager/Adapter is usually complicated and you may waste time trying to make it work.
On the other hand, you could add the Fragment to the ViewPager's Adapter and use the getType of the adapter to return a different type of Fragment.
So you could do (pseudo code):
mPagerAdapter.addFragmentDToTheDataAtPositionZero(); //longFancyName ;)
mPagerAdapter.notifyDataSetChanged();
mViewPager.setCurrentItem(0, false);
that'd add Fragment D to the "top" of the viewpager and will switch to it.
Your Adapter then has to use an Interface to determine what type of fragment must be instantiated…
The getItem of the adapter would look like… (again, pseudocode)
#Override
public Fragment getItem(final int i) {
final FragmentTypeInterface f = yourData.get(i);
if (f.isD()) {
return FragmentD.newInstance();
} else {
return OtherFragment.newInstance();
}
}
And so forth… :)