Android 使用Fragment界面向下跳转并一级级返回

一身转战三千里,一剑曾百万师。这篇文章主要讲述Android 使用Fragment界面向下跳转并一级级返回相关的知识,希望能为你提供帮助。
1.首先贴上项目结构图:

Android 使用Fragment界面向下跳转并一级级返回

文章图片

2.先添加一个接口文件BackHandledInterface.java,定义一个setSelectedFragment方法用于设置当前加载的Fragment在栈顶,主界面MainActivity须实现此接口,代码如下:
package com.example.testdemo; public interface BackHandledInterface {public abstract void setSelectedFragment(BackHandledFragment selectedFragment); }

3.定义一个抽象类BackHandledFragment继承自Fragment,后面跳转的Fragment界面都要继承自BackHandledFragment。抽象类BackHandledFragment中定义一个返回值为boolean类型的onBackPressed方法,用于处理点击返回按键(物理Back键)时的逻辑,若该方法返回false,表示当前Fragment不消费返回事件,而由Fragment所属的FragmentActivity来处理这个事件。代码如下:
 
1 package com.example.testdemo; 2 3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 6 public abstract class BackHandledFragment extends Fragment { 7 8protected BackHandledInterface mBackHandledInterface; 9 10/** 11* 所有继承BackHandledFragment的子类都将在这个方法中实现物理Back键按下后的逻辑 12*/ 13protected abstract boolean onBackPressed(); 14 15@Override 16public void onCreate(Bundle savedInstanceState) { 17super.onCreate(savedInstanceState); 18if (!(getActivity() instanceof BackHandledInterface)) { 19throw new ClassCastException( 20"Hosting Activity must implement BackHandledInterface"); 21} else { 22this.mBackHandledInterface = (BackHandledInterface) getActivity(); 23} 24} 25 26@Override 27public void onStart() { 28super.onStart(); 29// 告诉FragmentActivity,当前Fragment在栈顶 30mBackHandledInterface.setSelectedFragment(this); 31} 32 33 }

  4.主界面MainActivity要继承FragmentActivity才能调用getSupportFragmentManager()方法来处理Fragment。MainActivity还需重写onBackPressed方法用来捕捉返回键(Back Key)事件,代码如下:
 
1 package com.example.testdemo; 2 3 import android.os.Bundle; 4 import android.support.v4.app.FragmentActivity; 5 import android.support.v4.app.FragmentManager; 6 import android.support.v4.app.FragmentTransaction; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 11 public class MainActivity extends FragmentActivity implements 12BackHandledInterface { 13private static MainActivity mInstance; 14private BackHandledFragment mBackHandedFragment; 15private Button btnSecond; 16 17@Override 18public void onCreate(Bundle savedInstanceState) { 19super.onCreate(savedInstanceState); 20setContentView(R.layout.activity_main); 21btnSecond = (Button) findViewById(R.id.btnSecond); 22btnSecond.setOnClickListener(new OnClickListener() { 23 24@Override 25public void onClick(View v) { 26FirstFragment first = new FirstFragment(); 27loadFragment(first); 28btnSecond.setVisibility(View.GONE); 29} 30}); 31 32} 33 34public static MainActivity getInstance() { 35if (mInstance == null) { 36mInstance = new MainActivity(); 37} 38return mInstance; 39} 40 41public void loadFragment(BackHandledFragment fragment) { 42BackHandledFragment second = fragment; 43FragmentManager fm = getSupportFragmentManager(); 44FragmentTransaction ft = fm.beginTransaction(); 45ft.replace(R.id.firstFragment, second, "other"); 46ft.addToBackStack("tag"); 47ft.commit(); 48} 49 50@Override 51public void setSelectedFragment(BackHandledFragment selectedFragment) { 52this.mBackHandedFragment = selectedFragment; 53} 54 55@Override 56public void onBackPressed() { 57if (mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()) { 58if (getSupportFragmentManager().getBackStackEntryCount() == 0) { 59super.onBackPressed(); 60} else { 61if (getSupportFragmentManager().getBackStackEntryCount() == 1) { 62btnSecond.setVisibility(View.VISIBLE); 63} 64getSupportFragmentManager().popBackStack(); 65} 66} 67} 68 }

  5.分别添加两个子级Fragment,FirstFragment.java和SecondFragment.java,代码分别如下:
FirstFragment.java
 
1 package com.example.testdemo; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.support.v4.app.FragmentManager; 6 import android.support.v4.app.FragmentTransaction; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.ViewGroup; 11 import android.widget.Button; 12 13 public class FirstFragment extends BackHandledFragment { 14private View myView; 15private Button btnSecond; 16 17@Override 18public View onCreateView(LayoutInflater inflater, 19@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 20myView = inflater.inflate(R.layout.fragment_first, null); 21initView(); 22return myView; 23} 24 25private void initView() { 26btnSecond = (Button) myView.findViewById(R.id.btnSecond); 27btnSecond.setOnClickListener(new OnClickListener() { 28 29@Override 30public void onClick(View v) { 31SecondFragment second = new SecondFragment(); 32FragmentManager fm = getFragmentManager(); 33FragmentTransaction ft = fm.beginTransaction(); 34ft.replace(R.id.firstFragment, second); 35ft.addToBackStack("tag"); 36ft.commit(); 37} 38}); 39} 40 41@Override 42protected boolean onBackPressed() { 43return false; 44} 45 46 }

  SecondFragment.java
 
1 package com.example.testdemo; 2 3 import android.os.Bundle; 4 import android.support.annotation.Nullable; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 public class SecondFragment extends BackHandledFragment { 10 11private View mView; 12 13@Override 14public View onCreateView(LayoutInflater inflater, 15@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 16mView = inflater.inflate(R.layout.fragment_second, null); 17return mView; 18} 19 20@Override 21protected boolean onBackPressed() { 22return false; 23} 24 25 }

  6.三个布局文件代码如下:
activity_main.xml
 
1 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:orientation="vertical" > 6 7< TextView 8android:layout_width="wrap_content" 9android:layout_height="wrap_content" 10android:layout_centerInParent="true" 11android:text="FragmentActivity 父界面" 12android:textSize="26sp" /> 13 14< Button 15android:id="@+id/btnSecond" 16android:layout_width="wrap_content" 17android:layout_height="wrap_content" 18android:layout_alignParentBottom="true" 19android:text="跳转到FirstFragment" /> 20 21< FrameLayout 22android:id="@+id/firstFragment" 23android:layout_width="match_parent" 24android:layout_height="match_parent" > 25< /FrameLayout> 26 27 < /RelativeLayout>

   
fragment_first.xml
 
< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e5e5e5" android:orientation="vertical" > < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="FirstFragment" android:textColor="#000000" android:textSize="26sp" /> < Button android:id="@+id/btnSecond" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="打开SecondFragment" /> < /RelativeLayout>

   
fragment_second.xml
 
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:background="#e5e5e5" 6android:orientation="vertical" > 7 8< TextView 9android:layout_width="wrap_content" 10android:layout_height="wrap_content" 11android:layout_centerInParent="true" 12android:text="SecondFragment" 13android:textColor="#000000" 14android:textSize="26sp" /> 15 16 < /RelativeLayout>

   
7.最后奉上实例链接:
【Android 使用Fragment界面向下跳转并一级级返回】http://files.cnblogs.com/_ymw/TestDemo

    推荐阅读