Activity 到片段 1,片段 1 到片段 2 片段 2 到 Activity

Activity to fragment 1, fragment 1 to fragment 2 fragment 2 to Activity

我是 Android 开发的初学者。我正在创建一个小型应用程序,其中我从单个 activity 调用两个片段。 Activity -> 片段 1 -> 片段 2.

Activity到片段1,片段1到片段2。 我想知道我如何直接将片段 2 直接调用到 Activity。

我在 Fragment 2 中给出按钮,单击该按钮我想进入 Activity。

activity 已经存在。您的 activity 是托管片段的那个,即假设所有片段都是全屏片段,当您调用 fragment1 时,您的 activity 会删除当前片段(如果有的话)并用 fragment1 替换它,当您调用fragment2,fragment1替换为fragment2等。

如果您只想查看 activity 的布局(在大多数情况下这只是一个白屏),您必须删除所有片段,为此,将其添加到按钮的 onClick :

getActivity().getFragmentManager().popBackStack(1, FragmentManager.POP_BACK_STACK_INCLUSIVE);

根据你的问题,我注意到你仍然需要阅读有关片段的内容。 你不能从片段转到 activity 因为片段
是 activity 的一部分,它有自己的生命周期,接收自己的输入事件,您可以在 activity 是 运行 时添加或删除它们(有点像 "sub activity",您可以在不同的活动中重复使用)。

虽然您可以替换一个片段并在相同的 activity 上使用不同的片段。 你可以这样做:

  1. 首先在主要 xml 使用您要膨胀的布局:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/frgContainer"
    android:layout_margin="20dp"
    android:background="#00e6ff">
    

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn"
        android:text="btn"
        />
        </LinearLayout>
    
  2. 创建 2 个新活动,它们将成为我们的片段,带有 xml 文件,您可以向其中添加任何内容:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hackeru.mydynamicfragment.Login">
    
    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="User Name"
    android:id="@+id/txtLoginUser"
    android:layout_marginLeft="20sp"
    android:layout_marginRight="20sp"
    android:layout_marginTop="80dp"
    />
    
    <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Password"
    android:id="@+id/txtLoginPass"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    />
    
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnLogin"
    android:text="Login"
    />
    </LinearLayout>
    
  3. 覆盖片段上的 onCreate 方法

    public class Login extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_login,container,false);
    }
    

4.use main 中的 onClick 方法中的 fragmentTransaction 用您创建的片段替换或添加当前布局:

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

             FragmentManager fragmentManager =getFragmentManager();
        // we must handle the callback fragment process
        FragmentTransaction fragmentTransaction =
                fragmentManager.beginTransaction();
        Login loginFragment = new Login();
        fragmentTransaction.add(R.id.frgContainer,loginFragment);
         //  fragmentTransaction.replace if there is another fragment you
         //  wish to replace
        fragmentTransaction.commit(); 
}

读这个:

https://developer.android.com/guide/components/fragments.html