How to open or launch a Fragment from another Activity?

19,703

Solution 1

You cannot start a Fragment with Intents so the method that I recommend to you is :

Fragment mFragment = null;
mFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

For more information see Fragments Transactions documents

Solution 2

You can not start a Fragment using Intents. Intents provide a communication between activities.

If you want to launch a Fragment from other Fragment you have two options.

1- You can replace or add a new fragment.

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction
                .replace( R.id.content_frame, fragment )
                .addToBackStack( null )
                .commit();

In this case The activity has two fragments.

2- You can launch a new Activity that contains a Fragment.

Share:
19,703

Related videos on Youtube

Sebastian Delgado
Author by

Sebastian Delgado

Updated on September 15, 2022

Comments

  • Sebastian Delgado
    Sebastian Delgado over 1 year

    from the adapter of a RecyclerView which is contained in an Activity, i'm trying to launch a fragment when an element of the RecyclerView is pressed, this is my code right now :

    @Override
    public void onClick(View v) {
        Intent intent;
    
        int position = getAdapterPosition();
    
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        String onoff = preferences.getString("it's", "");
    
        if (onoff.equalsIgnoreCase("on")) {
            if (position == 0) {
    
                CategoriesRecyclerView.fragSubreddit = "aww";
    
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
    
            if (position == 1) {
    
                CategoriesRecyclerView.fragSubreddit = "food";
    
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
    
            if (position == 2) {
    
                CategoriesRecyclerView.fragSubreddit = "funny";
    
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
    
            if (position == 3) {
    
                CategoriesRecyclerView.fragSubreddit = "gaming";
    
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
    
        } else if (onoff.equalsIgnoreCase("off")) {
    
            if (position == 0) {
    
                CategoriesRecyclerView.fragSubreddit = "gaming";
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
    
            if (position == 1) {
    
                CategoriesRecyclerView.fragSubreddit = "funny";
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
    
            if (position == 2) {
    
                CategoriesRecyclerView.fragSubreddit = "food";
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
    
            if (position == 3) {
    
                CategoriesRecyclerView.fragSubreddit = "aww";
                intent = new Intent(context, MainFragment.class);
                context.startActivity(intent);
            }
        }
    }
    

    I Tested it launching some "testing activities" that i created, so i know that everything but the fragment launching works fine.

    The error is here :

    intent = new Intent(context, MainFragment.class);
    context.startActivity(intent);
    

    I'm launching the Fragment as it was an Activity, so when i run the app it crashes and tells me to declare the MainFragment as an activity in my manifest.

    How can i launch that Fragment from my Activity?

    Thanks very much.

  • Sebastian Delgado
    Sebastian Delgado over 8 years
    Thanks a lot, but what is getSupportFragmentManager() and R.id.content_frame, mFragment ?
  • Sebastian Delgado
    Sebastian Delgado over 8 years
    I don't want to star a fragment from another fragment, i want to star a fragment from an activity.
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 8 years
    Are you using v4Fragment? content frame is the content from your Fragment
  • Sebastian Delgado
    Sebastian Delgado over 8 years
    i am, but i still can't use the method getSupportFragmentManager() :(
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 8 years
    Remove the Support try with getFragmentManager()
  • Skizo-ozᴉʞS
    Skizo-ozᴉʞS over 8 years
    Sorry tried with context.getSupportFragmentManager()? Somehow you have to call your context first.
  • Sebastian Delgado
    Sebastian Delgado over 8 years
  • drearypanoramic
    drearypanoramic over 8 years
    @MustafaOlkun Yes, but how does one do that precisely?