当前位置:网站首页>Four Jump Ways of Fragment
Four Jump Ways of Fragment
2022-08-06 03:33:00 【SY_XLR】
This article mainly records aboutfragmentof four jumps方式:
1、从同一个Activiy的一个Fragment跳转到另外一个Fragment
2、从一个Activity的Fragment跳转到另外一个Activity
3、从一个Activity跳转到另外一个Activity的Fragment上
4、从一个Activity的Fragment跳转到另外一个Activity的Fragment上
Writing this article is just a simple record,When I was studying here, I always felt foggy when I read other people's articles,Later, I felt that it was almost enough,于是写下这篇博客,Also record your own learning process.
首先新建一个项目,Then create two new activitiesMainActivity、OtherActivity.
在MainActivityWrite a sub-layout in the layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>新建一个my_fragment.xml布局与MyFragment类
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyFragment"
android:textSize="40sp"
android:gravity="center_horizontal"/>
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="To YourFragment"/>
<Button
android:id="@+id/my_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="To OtherActivity"/>
</LinearLayout>MyFragmentThe class is temporarily omitted,All code will be posted later.
在MainActivityAdd one firstFragmentDo the initial presentation(Push-to-stack addition)
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
This jump is initially shown aboveFragment类似.
新建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.xmlIt is omitted for now,All the code will be posted at the end.
跳转部分代码如下,Jump by clicking the button:
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/** 一、从同一个Activity的一个Fragment跳到另外一个Fragment*/
//Push stack jump
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new YourFragment(), null)
.addToBackStack(null)
.commit();
}
});从一个Activity的Fragment跳转到另外一个Activity
This jump is related toActivityThe jumps between are very similar,Just refer to the context,改成getActivity()即可.
Jump key code:
myOther.setOnClickListener(new View.OnClickListener() {
/**
二、从一个Activity的Fragment跳转到另外一个Activity(等同于Activity之间的跳转(上下文是getActivity))
*/
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),OtherActivity.class);
startActivity(intent);
}
});从一个Activity跳转到另外一个Activity的Fragment上
我们要从OtherActivity跳转到MainActivity的YourFragment上去:
首先,我们在OtherActivitygiven in the jump event in MainActivity传递一个参数,命名为id:
Intent intent = new Intent(OtherActivity.this, MainActivity.class);
intent.putExtra("id",1);
startActivity(intent);
然后,我们在MainActivity里接收id值,对值进行判断,If the jump operation is performed correctly:
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.
This jump is very similar to the third jump,我们只需要将上面的
Intent intent = new Intent(OtherActivity.this, MainActivity.class);written in the correspondingFragment中,将OtherActivity.this更改为getActivity(),其他不用改变,to complete the jump.
关键代码如下:
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);
}
});
}所有代码文件
Finally attach all the code files.
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) {
/** 一、从同一个Activity的一个Fragment跳到另外一个Fragment*/
//Push stack jump
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new YourFragment(), null)
.addToBackStack(null)
.commit();
}
});
myOther.setOnClickListener(new View.OnClickListener() {
/**
二、从一个Activity的Fragment跳转到另外一个Activity(等同于Activity之间的跳转(上下文是getActivity))
*/
@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() {
/*从一个Activity跳转到另外一个Activity的Fragment上
例如我们要从OtherActivity跳转到MainActivity的YourFragment上去:
首先,我们在OtherActivitygiven in the jump event in MainActivity传递一个名为id的参数:
然后,我们在MainActivity里接收id值,对值进行判断,If the jump operation is performed correctly:
*/
@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() {
/*4、从一个Activity的Fragment跳转到另外一个Activity的Fragment上
This jump is very similar to the third jump,我们只需要将
Intent intent = new Intent(OtherActivity.this, MainActivity.class);
written in the correspondingFragment中,将OtherActivity.this更改为getActivity(),其他不用改变,Several completed jumps.
*/
@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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
activity_other.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/activity_other"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d0ff05"
>
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OtherActivity"
android:textSize="50sp"
android:gravity="center_horizontal"/>
<Button
android:id="@+id/to_MainActivity_YourFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To MainActivity YourFragment"
android:textAllCaps="false"/>
<Button
android:id="@+id/to_OtherFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To OtherFragment"
android:textAllCaps="false"/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
my_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyFragment"
android:textSize="40sp"
android:gravity="center_horizontal"/>
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="To YourFragment"/>
<Button
android:id="@+id/my_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="To OtherActivity"/>
</LinearLayout>other_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OtherFragment"
android:textSize="40sp"
android:gravity="center_horizontal"/>
<Button
android:id="@+id/to_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="To MainActivity YourFragment"/>
</LinearLayout>your_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0fa345">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="40sp"
android:text="YourFragment"/>
<Button
android:id="@+id/my_return"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RETURN"
/>
</LinearLayout>边栏推荐
- sql server , 两个表,相同字段,相同数据量(5000万), 执行相同的查询,一个表执行的是索引扫描,扫描5000万,耗时2分钟, 另一个表执行的是索引查找,只查找10万,耗时1秒。为什么会有这么大的差别?
- Sring中常用注解归纳
- Advantages and disadvantages of solid tires
- Classic sql example
- Mysql installation ask for answers
- 6.软件测试-----自动化测试之unittest框架
- 泡沫填充轮胎
- After the conversion goal in Google Analytics is set, how long does it take to display in the Google adwords backend?
- 椭圆曲线介绍(四):椭圆曲线安全性,与RSA对比
- 电赛的控制类选题之问
猜你喜欢

电赛电源类题型之问

If you like us, it is better to join us: come and contribute, the payment is reliable!

Introduction to Elliptic Curves (4): Elliptic Curve Security, Compared with RSA

xctf attack and defense world Web master advanced area easytornado

What is an Egg. The js, it have what features, installation of an Egg framework, definition of routing, what's Controller is the Controller, CORS configuration, the json configuration, a get request,

造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片

The delivery language set in the Google Ads background, if English is set, does it mean that the user is using English?

2022 Alibaba Cloud server configuration selection strategy

xctf Attack and Defense World Web Master Advanced Zone upload1

Advantages and disadvantages of solid tires
随机推荐
What are solid pneumatic tires?
七夕节微信表白墙小程序源码/基于Laravel的表白墙微信小程序源码
firewall and ufw notes
2022 使用Go语言构建现代 Web 应用程序实战内容课程
LabVIEW 应用程序视窗始终置于顶层
Single player up to 50,000 bonus!The top conference paper recurrence competition is officially launched, and 70+ public tasks are waiting for you to challenge
I understand atomic operations under linux
【Translation】Serverless Architecture: Pros and Cons
AC8015笔记
QT 中串口接收数据完整解决方案
foam filled tires
入坑机器学习:三,非监督学习
leetcode 18. Sum of Four Numbers
实心轮胎的优缺点
leetcode:20. 有效的括号
Fragment的四种跳转方式
Deep Learning Course2 Week 2 Hyperparameter tuning, Batch Normalization, Programming Frameworks exercises
离散数学期末习题
网络安全辅助工具:免费MD5解密网站
加密熊市为企业并购提供机遇 野心勃勃or救世主?唯一真理便是利益至上