0

I used this code below from one adapter class-

AuthorDetailsFragment fragment = new AuthorDetailsFragment();
                Bundle b = new Bundle();
                b.putString("authorId", "" + modelAllAuthors.get(pos).getId());

                fragment.setArguments(b);
                ((HomeActivity) context).addFragment(fragment, true);

This code works well. But when I am trying to call the fragment from another activity using same process it is not working. So, can I call the mentioned fragment from another activity?

Asif047
  • 21
  • 1
  • 1
  • 9

1 Answers1

0

You can use the following piece of code whenever you want to add a fragment.

   private void addFragment(@IdRes int containerViewId,
                           @NonNull Fragment fragment,
                           @NonNull String fragmentTag) {

    getSupportFragmentManager()
            .beginTransaction()
            .add(containerViewId, fragment, fragmentTag)
            .commit();
}
salmanseifian
  • 1,082
  • 3
  • 9
  • 26
  • can you please have a look to the question I have asked in the following link- https://stackoverflow.com/questions/53616758/how-can-we-call-one-fragment-from-another-class-that-doesnt-contain-that-fragme/53616870?noredirect=1#comment94095574_53616870 – Asif047 Dec 04 '18 at 16:56
  • The id of your container of the fragment like `R.id.container` – salmanseifian Dec 04 '18 at 17:01
  • yes, I called it inside one onClick event inside my adapter class- getSupportFragmentManager() .beginTransaction() .add(R.id.container_idea, fragment, "Ideas") .commit(); But it is not getting the getSupportFragmentManager() method – Asif047 Dec 04 '18 at 17:12