How to replace an activity fragment from the fragment itself?

There is a fragment in my application inside its action. I would like to programmatically replace a fragment with another from the current fragment itself.

For example, if I click on a button inside a fragment, the fragment should be replaced by another, but the action should remain the same.

Is it possible? If so, how to do it?

+49
android android-fragments
Nov 04
source share
6 answers

In fact, it is easy to invoke an operation to replace a fragment.

You need to click getActivity ():

((MyActivity) getActivity()) 

Then you can call methods from MyActivity, for example:

 ((MyActivity) getActivity()).replaceFragments(Object... params); 

Of course, this assumes that you have a replaceFragments () method in your activity that handles the fragment replacement process.

Edit: @ismailarilik added possible replaceFragments code in this code with the first comment below that was written by @ silva96:

The replaceFragments code can be:

 public void replaceFragments(Class fragmentClass) { Fragment fragment = null; try { fragment = (Fragment) fragmentClass.newInstance(); } catch (Exception e) { e.printStackTrace(); } // Insert the fragment by replacing any existing fragment FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.flContent, fragment) .commit(); } 
+42
Nov 04 '12 at 18:31
source share

from official documents:

 // Create new fragment and transaction Fragment newFragment = new ExampleFragment(); 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.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); 

In this example, newFragment replaces any fragment (if any) currently in the layout container identified by R.id.fragment_container . By calling addToBackStack() , the replaced fragment is saved in the background stack, so the user can cancel the transaction and return the previous fragment by clicking the "Back" button.

The behavior you described is exactly what fragments are for. Please read the official guide for a complete understanding of the fragments that will help you solve all your questions.

http://developer.android.com/guide/components/fragments.html

+53
Nov 04 '12 at 8:11
source share

Note that a fragment must NOT directly replace itself or any other fragments. Fragments must be separate objects. What fragment should do is to inform parent activity that some event has occurred. But again, this is NOT fragmentary work to decide what to do with it! This should be an action to make a decision about replacing a fragment on the phone, but, that is, adding another to the existing one on the tablets. So you basically do something wrong in design.

And, as mentioned earlier, your work should use the FragmentManager ("native" or from the compatibility library) to complete the task (for example, replace() or add() or remove() ):

http://developer.android.com/guide/components/fragments.html

+26
Nov 04 '12 at 18:48
source share

Just like Marsin said, you should not have a fragment starting with another fragment or activity. The best way to handle this situation is to create a callback implementation for the main action to process requests, such as starting a new fragment. Here is a great example in the Android Developer's Guide.

+3
Aug 27 '13 at 23:32
source share

There is a way that works; Just (in the snippet) do the following:

 getFragmentManager().beginTransaction() .add(R.id. container_of_this_frag, new MyNewFragment()) .remove(this) .commit(); 
0
Mar 02 '19 at 15:04
source share

When using nested fragments, we do not want each internal fragment replacement to go into the external most of the activity. A mechanism that allows a fragment to notify its parent that it wants to switch to another fragment may be useful.

Here is my Kotlin code, I think it is easy to translate to Java.

 interface FragmentNavigator { fun navigateTo(fragment: Fragment) } class NavigableFragment: Fragment() { var navigator: FragmentNavigator? = null override fun onDetach() { super.onDetach() navigator = null } } 

Inner fragments must extend NavigableFragment and use the following code to change themselves to another fragment.

 navigator?.navigateTo(anotherFragment) 

External actions or fragments must implement the FragmentNavigator and override navigateTo .

 override fun navigateTo(fragment: Fragment) { supportFragmentManager.beginTransaction().replace(view_id, fragment).commit() } //Use childFragmentManager instead of supportFragmentManager a fragment 

Finally, in external actions or fragments, override onAttachFragment

 override fun onAttachFragment(fragment: Fragment?) { super.onAttachFragment(fragment) if(fragment is NavigableFragment) { fragment.navigator = this } } 
0
May 6 '19 at 19:26
source share



All Articles