Aditya
Aditya

Reputation: 3673

How to add a fragment to activity, started from a fragment?

I am starting fragment A from Activity, then starting fragment B from fragment A. But the problem is that fragment B is not showing added when calling onActivityResult on fragment B. And the context of fragment B is also getting null.

Code to start fragment A from activity,

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentA fragment = new FragmentA();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commit();

Code to start fragment B from fragment A,

FragmentManager manager = getFragmentManager();
FragmentB fragment = new FragmentB();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, fragment).addToBackStack(null);
transaction.commit();

Upvotes: 2

Views: 6022

Answers (4)

Reyansh Mishra
Reyansh Mishra

Reputation: 1915

Use the below code in you activity's xml layout use FrameLayout as a container :-

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>

In your activity's java code write this code to add fragment

public void addFragment(Fragment fragment) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.main_parent, fragment);
            fragmentTransaction.commitAllowingStateLoss();
    }

Please note :-

fragmentTransaction.add(R.id.main_parent, fragment); 

will add the fragment in the container and

 fragmentTransaction .replace(R.id.main_parent, fragment);

will remove the previously added container and will add the new one HERE.

Now call the above function in your activity like this.

        TracksSubGridViewFragment tracksSubGridViewFragment = new TracksSubGridViewFragment();
addFragment(tracksSubGridViewFragment);

or in your fragment like this

TracksSubGridViewFragment tracksSubGridViewFragment = new TracksSubGridViewFragment();
     ((MainActivity) getActivity()).addFragment(tracksSubGridViewFragment);

Upvotes: 1

Sonu Sanjeev
Sonu Sanjeev

Reputation: 952

Instead of replacing fragment A with fragment B from fragment A, try replacing them in their activity itself.

((MyActivity) getActivity()).switchToSecondFragment(); 

And in the switchToSecondFragment method in activity :

public void switchToSecondFragment(){
     FragmentManager manager = getSupportFragmentManager();
     FragmentB fragment = new SelectTipFragment();
     FragmentTransaction transaction = manager.beginTransaction();
     transaction.replace(R.id.container, fragment).addToBackStack(null);
     transaction.commit();
}

Upvotes: 3

chandani c patel
chandani c patel

Reputation: 309

use below method for switching fragment from activity or fragment

public int switchContent(Fragment fragment, boolean isAddBackStack, String tag) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.container, fragment, tag);

        if (isAddBackStack) {
            ft.addToBackStack(null);
        }

        int commitID = ft.commitAllowingStateLoss();
        setCurrentTopFragment(Integer.parseInt(tag));
        return commitID;

    }

call this method like :

switchContent(mainListingFragment, false, String.valueOf(MAIN_LISTING_FRAGMENT));

int MAIN_LISTING_FRAGMENT = 1; // you can declare this in KeyInterface

public interface KeyInterface {
   int MAIN_LISTING_FRAGMENT = 1;
}

AND yes import following dependencies :

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

Upvotes: 0

AGM Tazim
AGM Tazim

Reputation: 2217

Use getSupportFragmentManager() instead of getFragmentManager() on FragmentA.

FragmentManager manager = getSupportFragmentManager();
FragmentB fragment = new SelectTipFragment();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, fragment).addToBackStack(null);
transaction.commit();

Upvotes: 0

Related Questions