Android从Fragment跳转到其他Activity的简单实例

编辑: admin 分类: 安卓教程 发布时间: 2022-03-14 来源:互联网

为了更好的理解以下内容,我们需要简单了解一下Fragment的动态注册方法

Android——Fragment的静态注册和动态注册

为了实现从Fragment跳转到其他Activity,下面需要创建以下文件:

第一步:简单编写布局文件fragment_activity.xml和抽象类TemplateFragmentActivity.java代码如下:

fragment_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/temp_fragment_activity"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
</FrameLayout>

fragment_activity.xml布局主要用于承载各fragment布局,例如fragment_one.xml和fragment_two.xml。

TemplateFragmentActivity.java

package com.example.myapplication;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public abstract class TemplateFragmentActivity extends AppCompatActivity
{
 private FragmentManager fm;
 private FragmentTransaction ts;
 private Fragment fragment;
 //抽象方法,用于创建Fragment实例
 protected abstract Fragment createFragment();
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.fragment_activity);
  fm = getSupportFragmentManager();
  ts = fm.beginTransaction();
  if (fragment == null){
   fragment = createFragment();
   ts.add(R.id.temp_fragment_activity,fragment);
   ts.commit();
  }
 }
}

第二步:分别使类FragmentOneActivity和FragmentTwoActivity继承类TemplateFragmentActivity并实现抽象方法createFragment()

FragmentOneActivity.java

package com.example.myapplication;
import androidx.fragment.app.Fragment;
public class FragmentOneActivity extends TemplateFragmentActivity {
 @Override
 protected Fragment createFragment() {
  return new FragmentOne();
 }
}

FragmentTwoActivity.java与FragmentOneActivity.java类似,不在重复。
第三步:分别编写fragment_one.xml和fragment_two.xml布局文件并通过编写FragmentOne.java和FragmentTwo.java绑定对应的布局文件,并实现其具体功能。

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:background="@color/colorPrimaryDark"
 android:orientation="vertical">
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="点击下面的按钮跳转到FragmentTwoActivity"
  android:textSize="20sp"
  android:textAllCaps="false"
  android:textColor="#F70505">
 </TextView>
 <Button
  android:id="@+id/btn_fm_one"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="跳转"
  android:textSize="30dp"
  android:layout_marginTop="20dp">
 </Button>
</LinearLayout>

fragment_two.xml与fragment_one.xml类似,不在重复。

FragmentOne.java

package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FragmentOne extends Fragment {
 private Button mBtnFragmentOne;
 @Nullable
 @Override
 public View onCreateView(@NonNull LayoutInflater inflater,
        @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
  View view = inflater.inflate(R.layout.fragment_one,container,false);
  mBtnFragmentOne = view.findViewById(R.id.btn_fm_one);
  mBtnFragmentOne.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent(getActivity(),FragmentTwoActivity.class);
    startActivity(intent);
   }
  });
  return view;
 }
}

Fragment跳转到Activity与Activity跳转到Activity方法类似,如下:

Intent intent = new Intent(getActivity(),FragmentTwoActivity.class);
startActivity(intent);

FragmentTwo.java与FragmentOne .java类似,不在重复。

演示·:

总结

以上所述是小编给大家介绍的Android从Fragment跳转到其他Activity的简单实例,希望对大家有所帮助!

更多阅读