在特定条件下加载片段

Loading a fragment upon a specific condition

我有问题请帮帮我。我正在构建一个 android 应用程序,并且已成功将奖励视频广告集成到其中。

我想在满足以下条件时显示片段或自动重定向到特定片段:

@Override
    public void onRewarded(RewardItem rewardItem) {
// I want to redirect the user to a particular fragment here. 
}

所以请帮助我,如果有任何其他方法可以实现此目的,请告诉我。

为此使用片段管理器和片段事务管理器。

getFragmentManager().beginTransaction().replace(R.id.your_framelayout_id, new your_fragment()).commit();

在你的 onRewarded 中写下下面的代码:

    android.support.v4.app.Fragment fragment = new FragmentA();
    android.support.v4.app.FragmentManager fragmentManager = getActivity()
                .getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = 
                fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();

此处 fragment_container 是在您的 Activity 中定义的 FrameLayout,您希望在其上托管您的片段。

我提到的示例 activity 带有框架布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Below is a fragment Container" />
     <FrameLayout
         android:id="@+id/fragment_container"
         android:layout_height="wrap_content"
         android:layout_width="match_content" />
</LinearLayout>