码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment监听返回键

时间:2015-04-15 12:58:42      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPressed(),所有BackHandledFragment的子类在onBackPressed方法中处理各自对Back事件的消费逻辑。onBackPressed返回布尔值,宿主FragmentActivity将会根据该方法的返回值判断子Fragment是否有消费Back事件。此外,宿主FragmentActivity还会保持一份当前Fragment的引用,当用户按下Back键时,宿主Activity会判断当前Fragment是否需要消费该事件,如果没有Fragment消费才会自己消费。

 1 public abstract class BackHandledFragment extends Fragment {
 2 
 3     protected BackHandledInterface mBackHandledInterface;
 4     
 5     /**
 6      * 所有继承BackHandledFragment的子类都将在这个方法中实现物理Back键按下后的逻辑
 7      * FragmentActivity捕捉到物理返回键点击事件后会首先询问Fragment是否消费该事件
 8      * 如果没有Fragment消息时FragmentActivity自己才会消费该事件
 9      */
10     protected abstract boolean onBackPressed();
11     
12     @Override
13     public void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         if(!(getActivity() instanceof BackHandledInterface)){
16             throw new ClassCastException("Hosting Activity must implement BackHandledInterface");
17         }else{
18             this.mBackHandledInterface = (BackHandledInterface)getActivity();
19         }
20     }
21     
22     @Override
23     public void onStart() {
24         super.onStart();
25         //告诉FragmentActivity,当前Fragment在栈顶
26         mBackHandledInterface.setSelectedFragment(this);
27     }
28     
29 }

宿主FragmentActivity需要继承BackHandledIntegerface,子Fragment会通过该接口告诉宿主FragmentActivity自己是当前屏幕可见的Fragment。

1 public interface BackHandledInterface {  
2     public abstract void setSelectedFragment(BackHandledFragment selectedFragment);  
3 }  

所以在Fragment的onCreate中会判断宿主FragmentActivity是否已继承了该接口。在Fragment的onStart()方法中就会调用该接口告诉宿主FragmentActivity自己是当前屏幕可见的Fragment。
宿主FragmentActivity就可以在onBackPressed()方法中对Back事件进行判断处理了。

 1 public class MainActivity extends FragmentActivity implements BackHandledInterface{  
 2   
 3     private BackHandledFragment mBackHandedFragment;  
 4     private boolean hadIntercept;  
 5   
 6     @Override  
 7     public void setSelectedFragment(BackHandledFragment selectedFragment) {  
 8         this.mBackHandedFragment = selectedFragment;  
 9     }  
10       
11     @Override  
12     public void onBackPressed() {  
13         if(mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()){  
14             if(getSupportFragmentManager().getBackStackEntryCount() == 0){  
15                 super.onBackPressed();  
16             }else{  
17                 getSupportFragmentManager().popBackStack();  
18             }  
19         }  
20     }  
21 }  

 

DEMO完整下载地址:https://github.com/mxy1228/FragmentHandleBackDemo/

http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/

Fragment监听返回键

标签:

原文地址:http://www.cnblogs.com/androidsj/p/4427906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!