Fragment中设置监听返回键的功能




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


package com.itheima.notelife1_1.fragment;
import com.itheima.notelife1_1.interfaces.BackHandledInterface;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;


public abstract class BaseFragment extends Fragment  {

	protected BackHandledInterface mBackHandledInterface;  
    
    /** 
     * 所有继承BackHandledFragment的子类都将在这个方法中实现物理Back键按下后的逻辑 
     * FragmentActivity捕捉到物理返回键点击事件后会首先询问Fragment是否消费该事件 
     * 如果没有Fragment消息时FragmentActivity自己才会消费该事件 
     */  
	public abstract boolean onBackPressed();  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        if(!(getActivity() instanceof BackHandledInterface)){  
            throw new ClassCastException("Hosting Activity must implement BackHandledInterface");  
        }else{  
            this.mBackHandledInterface = (BackHandledInterface)getActivity();  
        }  
    }  
      
    @Override  
    public void onStart() {  
        super.onStart();  
        //告诉FragmentActivity,当前Fragment在栈顶  
        mBackHandledInterface.setSelectedFragment(this);  
    }  

	public abstract void againSetToPageFuction();
	public abstract void againSetUpPageFuction();
	
}


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


package com.itheima.notelife1_1.interfaces;

import com.itheima.notelife1_1.fragment.BaseFragment;

public interface BackHandledInterface {
	public abstract void setSelectedFragment(BaseFragment selectedFragment);  
}

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



public class HomeActivity extends FragmentActivity implements BackHandledInterface {

	private BaseFragment mBackHandedFragment;  
    private boolean hadIntercept; 
	private int singn= 0;
	private FragmentManager manager;
	private Button main_common_add_bt;
	private Button main_private_bt;
	private Button main_manage_bt;
	private Button main_quest_net_bt;
	private GestureDetector gestureDetector;
	private SharedPreferences sp;
	private int keyback;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);

	}

	@Override
	public void setSelectedFragment(BaseFragment selectedFragment) {
		// TODO Auto-generated method stub
		this.mBackHandedFragment = selectedFragment; 
	}
	@Override
	public void onBackPressed() {
		 if(mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()){  
	            if(getSupportFragmentManager().getBackStackEntryCount() == 0){  
	                super.onBackPressed();  
	            }else{  
	                getSupportFragmentManager().popBackStack();  
	            }  
	        }  
	}

}




public class PrivateFragment extends BaseFragment {
	private FragmentManager fragmentManager;
	private ImageView iv_home_singbutton_pri;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
	}
	
	@Override
	public void againSetToPageFuction() {
	}

	@Override
	public void againSetUpPageFuction() {
		
	}
	@Override
	public boolean onBackPressed() {
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		transaction.replace(R.id.lv_home_replace, new MenuFragment());
		transaction.commit();
		return true;
	}
}







早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页