概述
本文主要记录了关于fragment的四种跳转方式:
1、从同一个Activiy的一个Fragment跳转到另外一个Fragment
2、从一个Activity的Fragment跳转到另外一个Activity
3、从一个Activity跳转到另外一个Activity的Fragment上
4、从一个Activity的Fragment跳转到另外一个Activity的Fragment上
写这篇文章只是一个简单的记录,当初我学这里的时候看别人的文章总是觉得云里雾里的,后来自己也觉得差不多可以了,于是写下这篇博客,也是记录自己的学习过程。
首先新建一个项目,然后新建两个活动MainActivity、OtherActivity。
在MainActivity的布局文件中写一个子布局:
<?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>
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) {
/** 一、从同一个Activity的一个Fragment跳到另外一个Fragment*/
//压栈式跳转
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new YourFragment(), null)
.addToBackStack(null)
.commit();
}
});
从一个Activity的Fragment跳转到另外一个Activity
此跳转与Activity之间的跳转十分相似,只要引用上下文的时候,改成getActivity()即可。
跳转关键代码:
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上去:
首先,我们在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) {
/** 一、从同一个Activity的一个Fragment跳到另外一个Fragment*/
//压栈式跳转
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上去:
首先,我们在OtherActivity中的跳转事件中给MainActivity传递一个名为id的参数:
然后,我们在MainActivity里接收id值,对值进行判断,如果正确进行跳转操作:
*/
@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上
这种跳转与第三种跳转极为类似,我们只需要将
Intent intent = new Intent(OtherActivity.this, MainActivity.class);
书写在对应的Fragment中,将OtherActivity.this更改为getActivity(),其他不用改变,几个完成跳转.
*/
@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>
参考:Android Fragment的四种跳转
最后
以上就是无私酒窝为你收集整理的Fragment的四种跳转方式的全部内容,希望文章能够帮你解决Fragment的四种跳转方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复