Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

java - How to open or launch a Fragment from another Activity?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...