如何从另一个活动中打开或启动片段? [英] How to open or launch a Fragment from another Activity?
问题描述
从一个Activity中包含的RecyclerView的适配器中,我试图在按下RecyclerView的某个元素时启动一个片段,这是我现在的代码:
@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);
}
}
}
我测试了它是否启动了我创建的一些测试活动",所以我知道除了片段启动之外的所有功能都可以正常工作.
错误在这里:
intent = new Intent(context, MainFragment.class);
context.startActivity(intent);
我正在启动Fragment,因为它是一个Activity,所以当我运行该应用程序时,它崩溃了,并告诉我在我的清单中将MainFragment声明为Activity.
如何从我的活动中启动该片段?
非常感谢.
您不能使用Intents
启动Fragment
,所以我向您推荐的方法是:
Fragment mFragment = null;
mFragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
有关更多信息,请参见片段交易文档
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.
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
这篇关于如何从另一个活动中打开或启动片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!