http://www.cnblogs.com/_ymw/p/4227862.html

1.首先贴上项目结构图:

2.先添加一个接口文件BackHandledInterface.java,定义一个setSelectedFragment方法用于设置当前加载的Fragment在栈顶,主界面MainActivity须实现此接口,代码如下:

  1. package com.example.testdemo;
  2.  
  3. public interface BackHandledInterface {
  4.  
  5. public abstract void setSelectedFragment(BackHandledFragment selectedFragment);
  6. }

3.定义一个抽象类BackHandledFragment继承自Fragment,后面跳转的Fragment界面都要继承自BackHandledFragment。抽象类BackHandledFragment中定义一个返回值为boolean类型的onBackPressed方法,用于处理点击返回按键(物理Back键)时的逻辑,若该方法返回false,表示当前Fragment不消费返回事件,而由Fragment所属的FragmentActivity来处理这个事件。代码如下:

  1. 1 package com.example.testdemo;
  2. 2
  3. 3 import android.os.Bundle;
  4. 4 import android.support.v4.app.Fragment;
  5. 5
  6. 6 public abstract class BackHandledFragment extends Fragment {
  7. 7
  8. 8 protected BackHandledInterface mBackHandledInterface;
  9. 9
  10. 10 /**
  11. 11 * 所有继承BackHandledFragment的子类都将在这个方法中实现物理Back键按下后的逻辑
  12. 12 */
  13. 13 protected abstract boolean onBackPressed();
  14. 14
  15. 15 @Override
  16. 16 public void onCreate(Bundle savedInstanceState) {
  17. 17 super.onCreate(savedInstanceState);
  18. 18 if (!(getActivity() instanceof BackHandledInterface)) {
  19. 19 throw new ClassCastException(
  20. 20 "Hosting Activity must implement BackHandledInterface");
  21. 21 } else {
  22. 22 this.mBackHandledInterface = (BackHandledInterface) getActivity();
  23. 23 }
  24. 24 }
  25. 25
  26. 26 @Override
  27. 27 public void onStart() {
  28. 28 super.onStart();
  29. 29 // 告诉FragmentActivity,当前Fragment在栈顶
  30. 30 mBackHandledInterface.setSelectedFragment(this);
  31. 31 }
  32. 32
  33. 33 }

4.主界面MainActivity要继承FragmentActivity才能调用getSupportFragmentManager()方法来处理Fragment。MainActivity还需重写onBackPressed方法用来捕捉返回键(Back Key)事件,代码如下:

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

5.分别添加两个子级Fragment,FirstFragment.java和SecondFragment.java,代码分别如下:

FirstFragment.java

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

SecondFragment.java

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

6.三个布局文件代码如下:
activity_main.xml

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

fragment_first.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#e5e5e5"
  6. android:orientation="vertical" >
  7.  
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_centerInParent="true"
  12. android:text="FirstFragment"
  13. android:textColor="#000000"
  14. android:textSize="26sp" />
  15.  
  16. <Button
  17. android:id="@+id/btnSecond"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_alignParentBottom="true"
  21. android:text="打开SecondFragment" />
  22.  
  23. </RelativeLayout>

fragment_second.xml

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

7.最后奉上实例链接:

http://files.cnblogs.com/_ymw/TestDemo

Android 使用Fragment界面向下跳转并一级级返回的更多相关文章

  1. [转]Android 使用Fragment界面向下跳转并一级级返回

      1.首先贴上项目结构图: 2.先添加一个接口文件BackHandledInterface.java,定义一个setSelectedFragment方法用于设置当前加载的Fragment在栈顶,主界 ...

  2. Android——实现欢迎界面的自动跳转(转)

    Android实现欢迎界面的自动跳转,就是打开某一个安卓手机应用,出现的欢迎界面停留几秒钟,自动进入应用程序的主界面.在网上看到很多种实现办法,但是感觉这种方法还是比较简单的. 在onCreate里设 ...

  3. Android横屏下Fragment界面重叠问题

    前言: 项目是基于平板开发的,设计的界面是要求横屏展示界面.所以我将所有的Activity都强制设置为横屏 android:screenOrientation="landscape" ...

  4. Android Fragment解析(下)

    今天被人问到了什么是Fragment,真是一头雾水,虽然以前也用到过,但不知道它是叫这个名字,狂补一下. 以下内容来自互联网,原文链接:http://blog.csdn.net/lmj62356579 ...

  5. Android Fragment学习笔记(二)----Fragment界面添加和管理

    Fragment界面添加 了解过fragment的生命周期等简单知识,于是去看官方文档来了解更多相关内容,要添加fragment到我们的UI界面中,给出了两种常用的方法,第一个是在activity的布 ...

  6. Android从Fragment跳转到Activity

    代码改变世界 Android从Fragment跳转到Activity Intent intent = new Intent(getActivity(), LoginActivity.class); s ...

  7. Android之Activity界面跳转--生命周期方法调用顺序

    这本是一个很基础的问题,很惭愧,很久没研究这一块了,已经忘得差不多了.前段时间面试,有面试官问过这个问题.虽然觉得没必要记,要用的时候写个Demo,打个Log就清楚了.但是今天顺手写了个Demo,也就 ...

  8. Android之Fragment(二)

    本文主要内容 如何管理Fragment回退栈 Fragment如何与Activity交互 Fragment与Activity交互的最佳实践 没有视图的Fragment的用处 使用Fragment创建对 ...

  9. Android开发——Fragment知识整理(一)

    0.  前言 Fragment,顾名思义是片段的意思,可以把Fragment当成Activity的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成.Fragment需要被嵌 ...

随机推荐

  1. lock模拟CountDownEvent

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. 怎样按xc或yc转正视图

    extern void create_view(void) { tag_t wcs_id,matrix_id; double mtx[9],wcs_pt[3]; double x_axis[3]={1 ...

  3. try-catch-finally的含有return使用揭秘

    很多人都会纠结这么一个问题try-catch-finally中有return的情况,我自己总结如下: 如果是值类型的话 请看代码 using System; using System.Collecti ...

  4. [VS2013]如何闪开安装VS2013必须要有安装IE10的限制

    来源:http://blog.163.com/qimo601@126/blog/static/1582209320143354446462/   已阻止安装程序,此版本的Visual Studio需要 ...

  5. 【洛谷P1941】飞扬的小鸟

    f [ i ] [ j ] 表示横坐标为 i ,高度为 j 时的最小点击次数 分别dp处理: 1.上升,(1)<m (2)>=m 2.下降 3.管道 #include<cstdio& ...

  6. jsp_属性范围_application

    如果希望设置一个属性,可以让所有用户看得见,则可以将属性范围设置成application,这样属性即可以保存在服务器上. 下面写一个小例子来验证一下: (1)application_demo.jsp ...

  7. SpringMVC上传文件

    SpringMVC中上传文件还是比较方便的,Spring内置了一些上传文件的支持类,不需要复杂的操作即可上传文件. 文件上传需要两个jar支持,一个是commons-fileupload.jar和co ...

  8. Spring MVC 指导文档解读(一)

    22.1 指导文档章节 In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, whic ...

  9. jquery_easyui 相关问题

    1. datagrid点击title,无法进行客户端排序. 增加属性 data-options="singleSelect:true,collapsible:true,url:'/ViewS ...

  10. [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...