Android如何从Fragment跳转到其他Activity的

发布时间:2021-10-15 16:41:06 作者:柒染
来源:亿速云 阅读:394

Android如何从Fragment跳转到其他Activity的,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

为了更好的理解以下内容,我们需要简单了解一下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类似,不在重复。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. Android实现Fragment跨Activity回调通信
  2. Activity与Fragment比较

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android fragment activity

上一篇:php怎么实现数组转object

下一篇:选择Linux虚拟主机的小技巧有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》