Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...