Fragment监听返回键

稍微进行了一起提取, 把公共方法写在了BaseUi中.

public abstract class BaseUi extends AppCompatActivity {
    protected Context mContext;
    protected KeyBackDownListener mKeyBackDownListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getApplicationContext;
        initView();
        initData();
        initListener();
    }

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode != KeyEvent.KEYCODE_BACK) {
            return true;
        }
        if (mKeyBackDownListener != null) {
            return mKeyBackDownListener.onKeyBackDownForFragment();
        } else {
            return onKeyBackDownForActivity(keyCode, event);
        }
    }

    public boolean onKeyBackDownForActivity(int keyCode, KeyEvent event) {
        return true;
    }

    public void addKeyDownListener(KeyBackDownListener keyBackDownListener) {
        mKeyBackDownListener = keyBackDownListener;
    }

    public void removeKeyDownListener() {
        mKeyBackDownListener = null;
    }
}
public interface KeyBackDownListener {
    boolean onKeyBackDownForFragment();
}
public class FragmentUi extends BaseUi {

    private AlertDialog mBackDialog;

    @Override
    protected void initView() {
        setContentView(R.layout.ui_fragment);
        TestFragment testFragment = new TestFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_frameLayout, testFragment).commit();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public boolean onKeyBackDownForActivity(int keyCode, KeyEvent event) {
        showExitDialog();
        return true;
    }

    private void showExitDialog() {
        mBackDialog = new AlertDialog.Builder(this).setMessage("确定要退出吗?")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mBackDialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mBackDialog.dismiss();
                    }
                })
                .show();
    }
}
public class TestFragment extends Fragment implements KeyBackDownListener, View.OnClickListener {

    private FragmentUi mFragmentUi;
    private Button mBtnAddKeyDownListener;
    private Button mBtnRemoveKeyDownListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mFragmentUi = (FragmentUi) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_test, null);
        mBtnAddKeyDownListener = (Button) rootView.findViewById(R.id.btnAddKeyDownListener);
        mBtnRemoveKeyDownListener = (Button) rootView.findViewById(R.id.btnRemoveKeyDownListener);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mFragmentUi.addKeyDownListener(this);
        mBtnAddKeyDownListener.setOnClickListener(this);
        mBtnRemoveKeyDownListener.setOnClickListener(this);
    }

    @Override
    public boolean onKeyBackDownForFragment() {
        LogUtils.log(getClass(), "onKeyDown()->1");
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAddKeyDownListener:
                mFragmentUi.addKeyDownListener(this);
                break;
            case R.id.btnRemoveKeyDownListener:
                mFragmentUi.removeKeyDownListener();
                break;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Activity 比较容易监听物理键返回事件(onBackPressed);Fragment却不能 假设一个Fra...
    初心一点阅读 8,504评论 0 4
  • 场景:在项目中做联系人界面时,需要按名字和按部门显示联系人,此处使用2个fragment切换显示,按部门显示需要体...
    joker_fu阅读 8,994评论 0 0
  • 有时候为了方便代码维护,在fragment中可能要处理一些监听fragment back键 以上代码是stacko...
    飞奔的蚂蚁阅读 3,994评论 0 0
  • 因为被吞了所以重发,其他被吞的修改后重发。 七夕特别篇,现代AU,私设众多,OOC没救,全员病入膏肓,杂叙文风。 ...
    稻香村拿破仑阅读 5,728评论 0 1
  • 你说过 你随时可以嫁给我 你说过 要和我一起去格陵兰岛 你说过 想每天听我唱歌 唱给你 你说过 我们要午睡在海边的...
    蒙脸阅读 760评论 0 1