How to replace the activity's fragment from the fragment itself?

change fragment layout programmatically
task affinity android
android clear activity stack programmatically
clear task android
android-intent flags explained

My application has a Fragment inside its Activity. I would like to programmatically replace the fragment by another one from the current fragment itself.

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

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

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) {
    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(, fragment)

Replace existing activity on top of back stack with a new instance of , The replacement feature allows you to quickly swap a client from one activity into another within the same program. PLEASE NOTE: If promotions or fee are tied� Here's how to change your move goal: Open the Activity app on your Apple Watch. Go to your rings, then firmly press the screen. Tap to increase or decrease the number of active calories for your daily Move goal. When you're done, tap Update. You can change the Move goal, but not your Exercise or Stand goals.

from the official docs:

// 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(, newFragment);

// Commit the transaction

In this example, newFragment replaces whatever fragment (if any) is currently in the layout container identified by the ID. By calling addToBackStack(), the replaced fragment is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button.

The behavior you have described is exactly what fragments are designed to do. Please go through the official guide for a thorough understanding of fragments which will clear up all your questions.

How to replace an activity, In order to replace one fragment with another, the activity's layout includes an empty FrameLayout that acts as the fragment container. Notice that the filename is� To change your Move goal, open the Activity app on your watch and press firmly on the screen, then tap on Change Move Goal. To edit settings for Activity app reminders (including Stand Reminders and Progress Updates): On your iPhone, in the Watch app, go to: My Watch (tab) > Activity.

Please note that fragment should NOT directly replace itself or any other fragments. Fragments should be separate entities. What fragment should do is to notify its parent activity that some event has happened. But it is, again, NOT a fragment job to decide what to do with that! It should be activity to decide to i.e. replace the fragment on phone, but to i.e. add another to existing one on tablets. So you are basically doing something wrong by design.

And, as others already mentioned, your activity should use FragmentManager ("native" or from compatibility library) to do the job (like replace() or add() or remove()):

Build a flexible UI, replace it with an underscore _ I am trying to use the Replace activity, but am not having success with special characters … transaction.replace() : Use another Fragment to replace current Fragment. transaction.hide() : Hide current Fragment, only make it invisible, not destroy. Then you can show it later.

Just as Marcin said, you shouldn't have a fragment start another fragment or activity. A better way to handle this situation is by creating a callback implementation for the main activity to handle requests such as start a new fragment. Here is a great example in the android developer guide.

Replace Activity - Build, Later in the video we will have a look at how to use the replace method Sir i want to close a Duration: 5:16 Posted: Nov 28, 2017 Tap a recent activity or search for an exercise type. Enter the activity details and tap Add. Now, in order to add the "Walk" to the Charge 2, please follow the instructions that @WendyB posted above and keep in mind that you can only have up to 7 activities, so if it is necessary, you'd have to delete one of them. Hope that helps!

There is a way which works; Just (in the fragment) do the following:

.add( container_of_this_frag, new MyNewFragment())

Fragments - Part 6, Add, Remove & Replace, Time spent watching television does not replace physical activity for Finnish men – unlike for Finnish women. A large proportion of highly active� Locate any Activities that have a notification icon next to them, these Activities need to be updated to include your replacement device. Select RE‑RUN ACTIVITY and add your replacement device. Perform the above steps until all of your Activities have been updated.

Time spent watching television does not replace physical activity for , Use 'Find and Replace' to change all the 'Humpty Dumpty' to 'Mighty Mac'. the skateboarding sessions will not be available during the Activity Week, but will� UiPath.Core.Activities.Replace Within a specified input string, replaces strings that match a regular expression pattern with a specified replacement string. This activity has a RegEx Builder wizard that can be used to configure it, on which you can read more here. Properties Input Pattern - The re

[DOC] Practice Exercises for 'Find and Replace' with MS Word, This operation creates or replaces a link between the specified activities. Activity links define the correlation between two activities. If a link is created between� The best thing about this is once you make this change, the Workout app will remember and start subsequent workouts in the unit of length of your choice. In addition to Workout app, the unit will also be changed in the Activity app both on the Apple Watch and the Activity app on iPhone.

REST API for Oracle Field Service Cloud Service, Create a new or replace an existing user activity for your app. If you'd like to create a user activity and its related **historyItems** in one request,� Green Belts are change agents who need to recognize, understand and interpret resistance to change and develop skills to manage it effectively. Managing change resistance is often covered in training, yet a primary learning issue facing most organizations is the lack of engagement and motivation in lecture-based training.