【问题标题】:Activity to fragment 1, fragment 1 to fragment 2 fragment 2 to ActivityActivity 到 Fragment 1,Fragment 1 到 Fragment 2 Fragment 2 到 Activity
【发布时间】:2016-05-17 10:12:17
【问题描述】:

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

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

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

【问题讨论】:

  • 不要在片段内部进行片段事务,您应该编写一个接口并在您的活动中实现该接口并在那里自己进行片段事务。

标签: android android-fragments fragment android-fragmentactivity


【解决方案1】:

活动已存在。您的活动是托管片段的活动,即假设所有片段都是全屏片段,当您调用片段1时,您的活动会删除当前片段(如果有)并将其替换为片段1,当您调用片段2时,片段1将替换为片段2和以此类推。

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

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

【讨论】:

    【解决方案2】:

    通过您的问题,我注意到您仍然需要阅读有关片段的内容。 您无法从片段转到活动,因为片段
    是 Activity 的一部分,有自己的生命周期,接收自己的输入事件,您可以在 Activity 运行时添加或删除它们(有点像“子 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.在main的onClick方法中使用fragmentTransaction,用你创建的fragment替换或添加当前布局:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 2012-11-27
      相关资源
      最近更新 更多