How to replace the activity's fragment from the fragment itself?
It's actually easy to call the activity to replace the fragment.
You need to cast getActivity():
((MyActivity) getActivity())
Then you can call methods from MyActivity, for example:
((MyActivity) getActivity()).replaceFragments(Object... params);
Of course, this assumes you have a replaceFragments() method in your activity that handles the fragment replace process.
Edit: @ismailarilik added the possible code of replaceFragments in this code with the first comment below which was written by @silva96:
The code of replaceFragments could 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();
}
Replacing a fragment from inside a fragment
Fragments are part of the Activity and fragment should be hosted through their parent activities as stated by google in Android developers docs.
You can refer this link:-
https://developer.android.com/training/basics/fragments/communicating
How to remove a fragment from within itself
You can remove the fragment instance like this
delete_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.remove(ActivityFragment.this).commit();
}
});
Android replace the current fragment with another fragment
Then provided your button is showing and the click event is being fired you can call the following in your click event:
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
and if you want to go back to the DetailsFragment on clicking back ensure you add the above transaction to the back stack, i.e.
ft.addToBackStack(null);
Or am I missing something? Alternatively some people suggest that your activity gets the click event for the button and it has responsibility for replacing the fragments in your details pane.
Replacing a Fragment with itself does not show anything
You can't replace a fragment with itself. The first half of a replace is a removal of the previous fragment at that id. Once a fragment is removed it can no longer be added or used by the fragment manager (so the add portion of the replace will not work properly).
Depending on your use case, you have two options:
- Create a new fragment instead of reusing the existing instance
- Use some other method to see if its necessary to replace your fragment
Finally, you probably don't need to call executePendingTransactions.
How to get a Fragment to remove itself, i.e. its equivalent of finish()?
What I don't understand is what to do in Df when 'OK' is pressed in order to remove fragments Df, Cf, and Bf?
Step #1: Have Df tell D "yo! we got the OK click!" via calling a method, either on the activity itself, or on an interface instance supplied by the activity.
Step #2: Have D remove the fragments via FragmentManager
.
The hosting activity (D) is the one that knows what other fragments are in the activity (vs. being in other activities). Hence, in-fragment events that might affect the fragment mix should be propagated to the activity, which will make the appropriate orchestration moves.
How to remove fragment from fragment itself
You missed add to back stack.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(frame.getId(), newFragment).addToBackStack(null).commit();
Related Topics
Generate Javadocs with Android Gradle Plugin
How to Enable Internal App Sharing for Android
How to Resolve Target 'Android-16'
Cannot Find R.Layout.Activity_Main
App Links Intent Filters in Assetlinks.JSON Not Working on Android
How to Get Results from an Intentservice Back into an Activity
Android: Radio Button in Custom List View
How to Know If Android Talkback Is Active
How to Get Total Ram Size of a Device
How to Properly Fire Action_Request_Ignore_Battery_Optimizations Intent
Android Viewmodel Has No Zero Argument Constructor
Imageview Adjustviewbounds Not Working
Uninstall App Silently with System Privileges
Handle Screen Orientation Changes When There Are Asynctasks Running