2

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();
Aditya
  • 3,525
  • 1
  • 31
  • 38

4 Answers4

3

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();
}
Sonu Sanjeev
  • 944
  • 8
  • 9
1

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);
Reyansh Mishra
  • 1,879
  • 1
  • 14
  • 26
0

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();
AGM Tazim
  • 2,213
  • 3
  • 16
  • 25
  • `getSupportFragmentManager` is not available on fragmentA. It showing only two methods `getChildFragmentManager` and `getFragmentManager` – Aditya Dec 28 '17 at 07:01
  • so. import `Fragment` from support library. not from `app`. import both `Fragment` from same `package` . either `app` or `support library`. check on `FragmentA` and `FragmentB`. – AGM Tazim Dec 28 '17 at 07:03
  • What's the difference in both ? – Aditya Dec 28 '17 at 07:07
  • If your `Fragment` is from `app` package then you should use `getFragmentManager() `but if from `support.v4` you should use `getSupportFragmentManager()`. Otherwise it will not work. check that first. I think `FragmentA` is from `android.support.v4.app.Fragment;`. But `FragmentB` from `android.app.Fragment`. – AGM Tazim Dec 28 '17 at 07:11
0

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;