I have a app they use Android.Support.V4.App.Fragment. Is it possible to launch a new Fragment from a "parent" Fragment?
For example Fragment A switch to Fragment B and when you press back from the Fragment B it change for the Fragment A.
Thanks.
This code will help you.
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.your_tab_id, new FragmentClassName())
.commitAllowingStateLoss();
Related
I have an activity with a fragment A inside of it. The fragment A have nested fragment B inside of it. I'm switching B with C using following code:
getChildFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.move_left_in, R.anim.move_left_out,R.anim.move_right_in, R.anim.move_right_out)
.replace(R.id.container, fragmentC)
.addToBackStack("nested")
.commit();
after that Im doing
getChildFragmentManager().popBackStack();
what brings me back to fragment B.
After that I switch fragment A with fragment D using code
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.move_up_in, R.anim.move_up_out, R.anim.move_down_in, R.anim.move_down_out)
.replace(R.id.fragment_holder, fragmentD)
.addToBackStack("fragments")
.commit();
and while that animation on A->D transaction is playing, the B plays animation of transaction C->B, and by poping backstack im getting same result, why?
When you switch fragment A with fragment D.
The content within fragment A detach from its parent and then transaction A -> D takes place,
Since you have already set animation to your fragments, delay due to those animation is the reason you,see that left out animation in B first,
then A -> D animation.
Don't use the getChildFragmentManager(),
Use support fragmnetManager means use getSupportFragmentManager why?
Bcz of nested fragment
I hope your problem is solved with this solution. Thank you:)
My App has only one activity and lots of fragments.
In my activty's XML, I just have a FrameLayout on which I replace/add/hide/show various fragments.
Imagine Fragment A is the first fragment the user sees when they open the app.
Click something in Fragment A to launch Fragment B and click something in Fragment B to launch Fragment C.
So the navigation is can be illustrated as follows :
Fragment A --> Fragment B --> Fragment C
I want to launch the app and show Fragment C directly from notification.
However, how can I provide back navigation from Fragment C, as such clicking back would go to Fragment B and clicking back again go to Fragment A ?
i.e How can I inject the following stack structure ?
Fragment A <-- Fragment B <-- Fragment C
Yes, you can do this. Since support library v26 you can build the stack with fragments without significant cost.
In your activity make the following:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentA())
.addToBackStack("fragmentA")
.setReorderingAllowed(true)
.commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentB())
.addToBackStack("fragmentB")
.setReorderingAllowed(true)
.commit();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentC())
.addToBackStack("fragmentC")
.setReorderingAllowed(true)
.commit();
Keep in mind that FragmentA and FragmentB will behave in a bit different way after pressing back on FragmentC due to setReorderingAllowed. onCreateView won't be called for FragmentA and FragmentB after they were added to stack, only in FragmentC onCreateView will be called. For FragmentA and FragmentB only onCreate will be called.
What you can do is -
Use a notification intent in which you pass a string. In your main activity if you receive that string make a fragment stack of A, B and C.
Else if you don't get the intent just continue your flow as it is.
So I have started fragment A from my main activity and that fragment contains a button. When that button is clicked this code runs :
Fragment newFragment = new HomeFragment();
// consider using Java coding conventions (upper first char class names!!!)
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.formFragment_Container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
So, as you can tell, I fire up fragment B from fragment A and what I don't get is what activity hosts that fragment B, since there is no indication of main activity in this code above, nor there is indication of fragment B in main activity. Could you clarify this for me. Thanks !
When you call to
getFragmentManager()
It use instance of FragmentManager that the Activity use to manage it Fragments. So the Activity will be the same as Activity A.
It comes under the concept of nested fragments in Android. For both Fragment A and B, its associated activity is main activity(from which you have added Fragment A).
You can access Main activity and Fragment A from Fragment B.
For example. getActivity() will return Main Activity, getParentFragment() will return Fragment A.
I am develop the app using the fragments and i am facing one fragment1 adding to the another fragment2
fragment1 onclicks performing fragment2 .I didn't findout the solution for that. please guide anyone know
I am using the following code to add the fragment
Fragment2 fragment2=new Fragment2();
FragmentManager fragmentManager = activity.getFragmentManager();
android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
ft.add(R.id.container,fragment2);
ft.hide(new Fragment1());
ft.addToBackStack(Fragment1.class.getName());
ft.commit();
The above to use adding onefragment1 adding to fragment2.Fragment1 onclicks perform to fragment2 . i didn't findout mistake.
please guide me anyone know .Advance thanks to all
nested Fragments are not recommended and for adding a fragment to a container from another Fragment use the parent Activity to do so , you can define a function inside Parent Activity which replace current Fragment with your second Fragment and call it from your first.
Adding a fragment to another fragment is the concept of nested fragments and not recommended. You should replace the fragment instead of adding. Use the following:
Fragment fragment = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().addToBackStack("fragment").replace(R.id.frame_container, fragment).commit();
When click the items of NavigationDrawer,The main activity container switch the fragments to show,It worked fine until I met this:
1.Switched to FragmentA, which CONTAINS A VIEWPAGER,it showed well.
2.Switched to fragmentB,fragmentB showed well.
3.Swiched back to
FragmentA,it shows as a Blank View
I tried to flip horizontally on it,I can see the viewpager index do changed(in log),But I don't know why it showed as a blank page.
*and,if fragmentA dose not contains viewpager,it worked well
Any suggestion would be appreciated.
I use replace() to switch between the fragments:
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, currentFragment)
.commit();
Possible reason might be not using replace transaction instead you mightbe adding fragments and adding it to backstack. Try using replace transaction, and see if it works.
Fragment1 firstFragment = new Fragment1();
Bundle bundle = new Bundle();
firstFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.article_fragment, firstFragment)
.commit();
Learn More on Fragments adding to back stack:
Android Fragment transaction: FragmentManager and Backstack