---------------------MainActivity-------------------
package com.example.earl.fragmentinterfacejump;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
/**
* 通过接口的方式实现fragment之间的跳转操作
*/
public class MainActivity extends Activity implements TestFragmentA.AbtnClikListener, TestFragmentB.BbtnonclickListener {
private TestFragmentA fragmentA;
private TestFragmentB fragmentB;
private TestFragmentC fragmentC;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = new TestFragmentA();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.content, fragmentA, "A");
ft.commit();
}
/**
* 实现TestFragmentA定义的接口
*/
@Override
public void onAbtnclick() {
if (fragmentB == null) {
fragmentB = new TestFragmentB();
fragmentB.setBbtnonclickListener(this);
}
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, fragmentB, "B");
/**
* 将当前的事务添加到了回退栈,所以TestFragmentA实例不会被销毁,但是视图层次依然会被销毁
* 在TestFragmentB界面中点击返回键可以返回TestFragmentA,而不是退出Activity
*/
ft.addToBackStack(null);
ft.commit();
}
/**
* 实现TestFragmentB定义的接口
*/
@Override
public void onBtnclick() {
if (fragmentC == null) {
fragmentC = new TestFragmentC();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, fragmentC, "C");
ft.addToBackStack(null);
ft.commit();
}
}
----------------------TestFragmentA------------------
package com.example.earl.fragmentinterfacejump;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* 跳转方式一
*/
public class TestFragmentA extends Fragment {
Button toB;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.e("TestFragment", "onCreateView");
View view = inflater.inflate(R.layout.layout_fragmenta, container, false);
toB = (Button) view.findViewById(R.id.toB);
toB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getActivity() instanceof AbtnClikListener){
((AbtnClikListener) getActivity()).onAbtnclick();
}
}
});
return view;
}
public interface AbtnClikListener {
void onAbtnclick();
}
}
--------------------TestFragmentB--------------------
package com.example.earl.fragmentinterfacejump;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* 跳转方式二
*/
public class TestFragmentB extends Fragment implements View.OnClickListener{
Button toC;
private BbtnonclickListener bbtnonclickListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.e("TestFragment", "onCreateView");
View view = inflater.inflate(R.layout.layout_fragmentb, container, false);
toC = (Button) view.findViewById(R.id.toC);
toC.setOnClickListener(this);
return view;
}
public void setBbtnonclickListener(BbtnonclickListener bbtnonclickListener) {
this.bbtnonclickListener = bbtnonclickListener;
}
@Override
public void onClick(View v) {
if (bbtnonclickListener != null) {
bbtnonclickListener.onBtnclick();
}
}
public interface BbtnonclickListener {
void onBtnclick();
}
}
-------------------TestFragmentC------------------
package com.example.earl.fragmentinterfacejump;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2017/8/4.
*/
public class TestFragmentC extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.e("TestFragment", "onCreateView");
return inflater.inflate(R.layout.layout_fragmentc, container, false);
}
}
------------------activity_main.xml----------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</android.support.constraint.ConstraintLayout>
-------------------layout_fragmenta.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:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是页面A" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/toB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="toB" />
</LinearLayout>
-------------------layout_fragmentb.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:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是页面B" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/toC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="toC" />
</LinearLayout>
-------------------layout_fragmentc.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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是页面C" />
</LinearLayout>