如何从其他活动中打开或启动片段?

时间:2015-09-13 19:37:26

标签: java android android-fragments android-intent android-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声明为我的清单中的一个活动。

如何从我的活动中启动片段?

非常感谢。

2 个答案:

答案 0 :(得分:3)

您无法使用Fragment启动Intents,因此我向您推荐的方法是:

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

有关详细信息,请参阅Fragments Transactions documents

答案 1 :(得分:0)

如果您使用的是Sections Pager Adapter,那么简单的操作就是使用onclick在图像视图或按钮上,如下所示:

在开始活动中(您想去的地方),请使用多余的内容将信息发送到另一个活动中

ivPhotoAlbums.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        // Send The Message Receiver ID & Name
        Intent intent = new Intent(v.getContext(), PhotosActivity.class);
        intent.putExtra("viewpager_position", 1);
        v.getContext().startActivity(intent);

    }
});

在您要登陆的活动中(在setupViewPager()函数的末尾),捕捉到了多余的东西并将其用作位置

int position = 0;
Bundle extras = getIntent().getExtras();
if (extras != null) {
    position = extras.getInt("viewpager_position");
}
viewPager.setCurrentItem(position);

假定您的目标活动中的setupViewPager()函数如下所示

SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());

adapter.addFragment(new FriendsAlbumsFragment()); //index 0
adapter.addFragment(new MyAlbumsFragment()); //index 1
adapter.addFragment(new AddPhotosFragment()); //index 2

final ViewPager viewPager = findViewById(R.id.l_center_view_pager_view_pager);
viewPager.setAdapter(adapter);

TabLayout tabLayout = findViewById(R.id.l_top_tabs_tabs);

tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_friends_photos);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_my_photos);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_add_photos);

您的SectionsPagerAdapter类如下(单独的公共类)

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> mFragmentList = new ArrayList<>();

    public SectionsPagerAdapter(FragmentManager fm) {

        super(fm);

    }

    @Override
    public Fragment getItem(int i) {

        return mFragmentList.get(i);
    }

    @Override
    public int getCount() {

        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment) {

        mFragmentList.add(fragment);
    }
}

您的中心视图传呼机布局为

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v4.view.ViewPager
        android:id="@+id/l_center_view_pager_view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="56dp">

    </android.support.v4.view.ViewPager>

</merge>

包含在这样的另一个文件中(或不包含-在我的情况下,它包含在内)

<include layout="@layout/layout_center_viewpager" />

这将使您从一个活动转到一个片段(在另一个活动中),或者从一个活动中的片段到另一个活动中的片段...