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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…