本文章向大家介绍怎么在Android中实现从Fragment跳转到其他Activity,主要包括怎么在Android中实现从Fragment跳转到其他Activity的使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
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的文章就介绍到这了,更多相关的内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。