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
Related
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()
Basically, I want to open a fragment from Activity. Everything worked fine (activity button opens the fragment and the fragment shows), until I miss-clicked outside the fragment's layout where one of the Activity button is located and it did its onClick method. That's why I wondered why is my Activity still active.
this is how I open the fragment from Activity
I tried using .add instead of .replace still the same.
#OnClick(R.id.login_register_lbl)
public void registerAction(View view) {
// TODO register transition...
FragmentManager fragmentManager = (LoginActivity.this.getFragmentManager());
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
RegisterFragment fragment = new RegisterFragment();
fragmentTransaction.replace(R.id.fragment_register_holder, fragment);
fragmentTransaction.commit();
}
In fragment layout in root element set "background" , "clickable=true" & Focusable="true".
In my opinion you can achieve this functionality by 2 ways:
Open a dialog fragment in full screen mode to avoid accidentally clicks on activities views.
If you wanted to show both fragment's and activity's views at the same time then, on click of your activity's button which opens the fragment, disable it by yourbutton.setenabled(false); and when you wanted to close the fragment enable that button again.
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...
}
I want to start a new activity inside the fragment boundaries itself instead of loading it in a complete new full screen.
I tried this :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Intent intent = new Intent(this,MenuFragmentActivity.class);
MenuFragment newFragment = new MenuFragment();
fragmentTransaction.add(R.id.menuFragment,newFragment);
newFragment.startActivity(intent);
fragmentTransaction.commit();
But this starts the activity in a new screen rather than in the confined fragment ?
The behavior you're seeing is correct.
For layout purposes, activities cannot be "children" of fragments. It's the other way around: fragments are children of activities. So, basically, what you're trying to do won't work.
You should read the full Fragments guide if you haven't already. Here's the relevant quote about layouts:
When you add a fragment as a part of your activity layout, it lives in
a ViewGroup inside the activity's view hierarchy and the fragment
defines its own view layout. You can insert a fragment into your
activity layout by declaring the fragment in the activity's layout
file, as a <fragment> element, or from your application code by adding
it to an existing ViewGroup.
Instead of starting a new activity, try just loading another fragment within the original fragment's layout.
I have used View pager in my app for my list fragments (A,B,C,D). On clicking on an item in listview (of say Fragment A) , a detailed fragment (E) is shown. Fragment E is not part of view pager for obvious reasons.
At this point, if i use the getFragments() method of fragmentmanager, i only get the view pager fragments (A,B,C,D) and not E. On iterating through view pager fragments,one of the fragment of view pager comes out to be visible,although the fragment E is on top and visible.
What is the correct way of replacing a view pager fragment with a non-view pager fragment? Why is Fragment E, not added to fragment manager stack?
Here is the code segment that i use while replacing Fragment A with Fragment E.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.detach(fragA);
fragmentTransaction.add(viewId, fragE);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Why i am doing this is, because on some async calls completion, i would like to refresh the fragment which is currently visible to user.
Thanks,
Rony
have you tried replace?
FragmentTransaction fragmentTransaction =getActivity().getSupportFragmentManager().beginTransaction();
// Replace the fragment in the container view with the E fragment,
// and add the transaction to the back stack
fragmentTransaction.replace(R.id.container,FragmentE.newInstance(),MainActivity.FRAGMENT_TAG_E);
fragmentTransaction.addToBackStack(null); // Commit the transaction
fragmentTransaction.commit();
Replacing the fragments looks to be an inefficient solution as it recreates the fragment unnecessarily. For now I have solved it by maintaining a list of my own for all the fragments.