广告
返回顶部
首页 > 资讯 > 移动开发 >Fragment的四种跳转方式
  • 507
分享到

Fragment的四种跳转方式

android 2023-09-08 15:09:29 507人浏览 泡泡鱼
摘要

本文主要记录了关于fragment的四种跳转方式:   从同一个Activiy的一个Fragment跳转到另外一个Fragment   2、从一个Activity的Fragment跳转到另外一个Activity   3、从一个Activ

本文主要记录了关于fragment的四种跳转方式:  

从同一个Activiy的一个Fragment跳转到另外一个Fragment  
2、从一个Activity的Fragment跳转到另外一个Activity  
3、从一个Activity跳转到另外一个Activity的Fragment上
4、从一个Activity的Fragment跳转到另外一个Activity的Fragment上

写这篇文章只是一个简单的记录,当初我学这里的时候看别人的文章总是觉得云里雾里的,后来自己也觉得差不多可以了,于是写下这篇博客,也是记录自己的学习过程。

首先新建一个项目,然后新建两个活动MainActivity、OtherActivity。
在MainActivity的布局文件中写一个子布局:

    

新建一个my_fragment.xml布局与MyFragment类

        

MyFragment类就暂时省略了,后面会贴出所有代码。
在MainActivity中先添加进一个Fragment进行最开始的展示(压栈式添加)

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        getSupportFragmentManager()                .beginTransaction()                .replace(R.id.fragment_container,new MyFragment())                .addToBackStack(null)                .commit();    }}

从同一个Activiy的一个Fragment跳转到另外一个Fragment

这个跳转与上面初始显示Fragment类似。
新建your_fragment.xml布局与YourFragment类。

public class YourFragment extends Fragment {    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View contentView;        contentView = inflater.inflate(R.layout.your_fragment, container, false);        return contentView;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        Button myReturn = (Button) getActivity().findViewById(R.id.my_return);        myReturn.setOnClickListener(new View.OnClickListener() {            //返回到上一个Fragment(同一个Activity中)            @Override            public void onClick(View v) {                getActivity().getSupportFragmentManager().popBackStack();            }        });    }}

your_fragment.xml就暂时先省略了,最后会贴出全部代码。

跳转部分代码如下,通过点击按钮跳转:

myButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                                //压栈式跳转                getActivity().getSupportFragmentManager()                        .beginTransaction()                        .replace(R.id.fragment_container, new YourFragment(), null)                        .addToBackStack(null)                        .commit();            }        });

从一个Activity的Fragment跳转到另外一个Activity

此跳转与Activity之间的跳转十分相似,只要引用上下文的时候,改成getActivity()即可。

跳转关键代码:

myOther.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                Intent intent = new Intent(getActivity(),OtherActivity.class);                startActivity(intent);            }        });

从一个Activity跳转到另外一个Activity的Fragment上

我们要从OtherActivity跳转到MainActivity的YourFragment上去:
首先,我们在OtherActivity中的跳转事件中给MainActivity传递一个参数,命名为id:

Intent intent = new Intent(OtherActivity.this, MainActivity.class);intent.putExtra("id",1);startActivity(intent);

 然后,我们在MainActivity里接收id值,对值进行判断,如果正确进行跳转操作:

int id = getIntent().getIntExtra("id", 0);if (id == 1) {           getSupportFragmentManager()       .beginTransaction()       .replace(R.id.fragment_container,new YourFragment())       .addToBackStack(null)       .commit(); }

从一个Activity的Fragment跳转到另外一个Activity的Fragment上

新建other_fragment.xml布局作为OtherActivity的一个Fragment。

 这种跳转与第三种跳转极为类似,我们只需要将上面的

Intent intent = new Intent(OtherActivity.this, MainActivity.class);

书写在对应的Fragment中,将OtherActivity.this更改为getActivity(),其他不用改变,就能完成跳转。

关键代码如下:

public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        Button ToButton = (Button) getActivity().findViewById(R.id.to_button);        ToButton.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                Intent intent = new Intent(getActivity(), MainActivity.class);                intent.putExtra("id",1);                startActivity(intent);            }        });    }

所有代码文件

最后附上所有的代码文件。  
MainActivity:

package com.example.fragment_activity_skiptest;import Android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        getSupportFragmentManager()                .beginTransaction()                .replace(R.id.fragment_container,new MyFragment())                .addToBackStack(null)                .commit();        int id = getIntent().getIntExtra("id", 0);        if (id == 1) {            getSupportFragmentManager()                    .beginTransaction()                    .replace(R.id.fragment_container,new YourFragment())                    .addToBackStack(null)                    .commit();        }    }}

MyFragment:

package com.example.fragment_activity_skiptest;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;public class MyFragment extends Fragment {    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View contentView;            contentView = inflater.inflate(R.layout.my_fragment, container, false);        return contentView;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        Button myButton = (Button) getActivity().findViewById(R.id.my_button);        Button myOther = (Button) getActivity().findViewById(R.id.my_other);        myButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                                //压栈式跳转                getActivity().getSupportFragmentManager()                        .beginTransaction()                        .replace(R.id.fragment_container, new YourFragment(), null)                        .addToBackStack(null)                        .commit();            }        });        myOther.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                Intent intent = new Intent(getActivity(),OtherActivity.class);                startActivity(intent);            }        });    }}

OtherActivity:

package com.example.fragment_activity_skiptest;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class OtherActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_other);        Button button = (Button)findViewById(R.id.to_MainActivity_YourFragment);        Button button_back = (Button)findViewById(R.id.back);        Button button_fm = (Button)findViewById(R.id.to_OtherFragment);        button.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                Intent intent = new Intent(OtherActivity.this, MainActivity.class);                intent.putExtra("id",1);                startActivity(intent);            }        });        button_back.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                finish();            }        });        button_fm.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                getSupportFragmentManager()                        .beginTransaction()                        .replace(R.id.frame_container, new OtherFragment(), null)                        .addToBackStack(null)                        .commit();            }        });    }}

OtherFragment:

package com.example.fragment_activity_skiptest;import android.content.Intent;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;public class OtherFragment extends Fragment {    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View contentView;        contentView = inflater.inflate(R.layout.other_fragment, container, false);        return contentView;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        Button ToButton = (Button) getActivity().findViewById(R.id.to_button);        ToButton.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                Intent intent = new Intent(getActivity(), MainActivity.class);                intent.putExtra("id",1);                startActivity(intent);            }        });    }}

YourFragment:

package com.example.fragment_activity_skiptest;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;public class YourFragment extends Fragment {    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View contentView;        contentView = inflater.inflate(R.layout.your_fragment, container, false);        return contentView;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        Button myReturn = (Button) getActivity().findViewById(R.id.my_return);        myReturn.setOnClickListener(new View.OnClickListener() {            //返回到上一个Fragment(同一个Activity中)            @Override            public void onClick(View v) {                getActivity().getSupportFragmentManager().popBackStack();            }        });    }}

activity_main.xml:

    

activity_other.xml:

                                    

my_fragment.xml:

        

other_fragment.xml:

        

your_fragment.xml:

        

参考:Android Fragment的四种跳转

来源地址:https://blog.csdn.net/m0_61465701/article/details/126145181

--结束END--

本文标题: Fragment的四种跳转方式

本文链接: https://www.lsjlt.com/news/400203.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Fragment的四种跳转方式
    本文主要记录了关于fragment的四种跳转方式:   从同一个Activiy的一个Fragment跳转到另外一个Fragment   2、从一个Activity的Fragment跳转到另外一个Activity   3、从一个Activ...
    99+
    2023-09-08
    android
  • 教你nginx跳转配置的四种方式
    目录前言一、配置server对应的域名1.1、精确匹配1.2、正则表达式二、配置location2.1、Location 匹配规则:仅匹配URI,忽略参数2.2、举例2.3、匹配顺序...
    99+
    2022-11-13
  • JS跳转几种方式
    在JS中,有多种方式可以实现页面跳转。以下是常见的几种方式:1. 使用location对象的href属性:可以通过修改locatio...
    99+
    2023-09-15
    JS
  • php的跳转方式有几种
    这篇文章主要介绍“php的跳转方式有几种”,在日常操作中,相信很多人在php的跳转方式有几种问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php的跳转方式有几种”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-06-20
  • 详解SpringMVC中的四种跳转方式、视图解析器问题
    目录一、视图解析器:1.springmvc核心配置文件,添加视图解析器:2.视图解析器的使用:3、视图解析器类InternalResourceViewResolver源码解析:二、S...
    99+
    2022-11-13
    SpringMVC跳转方式 SpringMVC视图解析器
  • vue3页面跳转的两种方式
    目录1、标签内 router-link跳转2、编程式路由导航vue3的页面跳转有两种方式,第一种是标签内跳转,第二种是编程式路由导航 1、 <router-link to='/...
    99+
    2023-05-20
    vue3页面跳转 vue跳转页面
  • C++强制类型转换的四种方式
    目录1 C++类型转换本质1.1 自动类型转换(隐式)1.2 强制类型转换(显式)1.3 类型转换的本质1.4 类型转换的安全性2 四种类型转换运算符2.1 C语言的强制类型转换与C...
    99+
    2022-11-13
  • Java 数组转List的四种方式小结
    目录第一种方式(未必最佳):使用ArrayList.asList(strArray)第二种方法(支持增删查改):第三种方式(通过集合工具类Collections.addAll()方法...
    99+
    2022-11-12
  • Vue路由跳转的4种方式小结
    目录1、 标签路由 router-link2、编程式路由 this.$router.push()3、this.$router.replace()(与this.$router.push...
    99+
    2022-11-13
  • JavaScript实现页面跳转的八种方式
    整理一下JavaScript八种跳转方式,欢迎评论补充! 第一种方法: <script> window.location.replace('http://www....
    99+
    2022-11-13
  • 基于React路由跳转的几种方式
    目录React路由跳转的几种方式1. params形式2. 使用state的形式React路由跳转传参问题使用Link传参url传参隐式传参React路由跳转的几种方式 注意: 这里...
    99+
    2022-11-13
  • Android中activity跳转按钮事件的四种写法
    具体实现代码: public class MainActivity extends Activity { @Override protected void onCreat...
    99+
    2022-06-06
    事件 activity 按钮 Android
  • H5页面跳转小程序的三种方式
    文章目录 前言一、web-view标签返回小程序1.小程序启动页面只写web-view标签跳转到授权页面。2.编写auth.html3、把auth.html放到服务器就可以测试访问,打开小程序默认进入启动页面中的webview跳转到...
    99+
    2023-08-16
    小程序 微信小程序 前端 微信 html5
  • springboot的四种启动方式
    目录环境准备第一种:直接main方法启动TxDemo2Application第二种:通过maven插件来启动第三种 打jar包来访问第四种 通过docker容器虚拟化运行环境准备 创...
    99+
    2022-11-13
  • 修改NLS_DATE_FORMAT的四种方式
    一、 在用户环境变量中指定(LINUX) 在用户的.bash_profile中增加两句: export NLS_LANG=AMERICAN ---这一句必须指定,否则下一句不生效。 ex...
    99+
    2022-10-18
  • Java的四种引用方式
    目录1.强引用(StrongReference)2.软引用(SoftReference)3.弱引用(WeakReference)4.虚引用(PhantomReference)5.&n...
    99+
    2022-11-12
  • 详解Android中Fragment的两种创建方式
    fragment是Activity中用户界面的一个行为或者是一部分。你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activ...
    99+
    2022-06-06
    fragment Android
  • uni-app常用的几种页面跳转方式总结
    目录一、uni.navigateTo(OBJECT)二、uni.navigateBack(OBJECT)三、uni.redirectTo(OBJECT)四、uni.switchTab...
    99+
    2022-11-13
  • 线程同步的四种方式
    线程同步的四种方式包括:1. 互斥锁:使用互斥锁(Mutex)来保证在同一时刻只有一个线程访问共享资源。当一个线程获取到互斥锁后,其...
    99+
    2023-09-14
    线程
  • python 调用js的四种方式
    目录1. 前言2. 准备3. 方式一:PyExecJS4. 方式二:js2py5. 方式三:Node.js6. 方式四:PyV87. 最后1. 前言 日常 Web 端爬虫过程中,经...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作