回调函数

本文介绍了一种在Android应用中自定义处理Back按键的方法,通过定义IBackPressedListener接口并实现在多个Activity中,实现统一的Back按键行为控制。此外,还展示了如何封装工具类UserUtil,用于更新关注状态,并通过回调函数UpdateFollowCallback传递操作结果。

1、IBackPressedListener

1)MainActivity定义接口

public class FileExplorerTabActivity extends Activity {
 
    //...do something...
    public interface IBackPressedListener {
        /**
         * 处理back事件。
         * @return True: 表示已经处理; False: 没有处理,让基类处理。
         */
        boolean onBack();
    }
 
    @Override
    public void onBackPressed() {
        IBackPressedListener backPressedListener = (IBackPressedListener) mTabsAdapter
                .getItem(mViewPager.getCurrentItem());
        if (!backPressedListener.onBack()) {
            super.onBackPressed();
        }
    }
}

2)其他Activity使用接口

public class FileViewActivity extends Fragment implements IBackPressedListener {
 
    //...do something...
    @Override
    public boolean onBack() {
        if (mBackspaceExit || !Util.isSDCardReady() || mFileViewInteractionHub == null) {
            return false;
        }
        return mFileViewInteractionHub.onBackPressed();//返回FileViewInteractionHub类的处理结果
    }
}


3)FileViewInteractionHub.java

public class FileViewInteractionHub {
    //...do something...
    public boolean onBackPressed() {
        if (mDropdownNavigation.getVisibility() == View.VISIBLE) {
            mDropdownNavigation.setVisibility(View.GONE);
        } else if (isInSelection()) {
            clearSelection();
        } else if (!onOperationUpLevel()) {
            return false;
        }
        return true;
    }
}

2、封装工具类,传递回调函数结果

1)UserUtil.java

public interface UpdateFollowCallback {
        public void updateFollow(int resultCode, FollowUpdateResult result);
    }
 
 
    public static void updateFollow(Context context, final String user_Id,
                                    boolean toCancel, final UpdateFollowCallback updateFollowCallback) {
 
        IBeanLoader iBeanLoader = new BeanLoaderImpl(context.getApplicationContext());
        if (toCancel) {
            iBeanLoader.loadHttp(new FollowUpdateBean(user_Id, TO_CANCEL_FOLLOW));
        } else {
            iBeanLoader.loadHttp(new FollowUpdateBean(user_Id, TO_FOLLOW));
        }
        iBeanLoader.setCallback(new IBeanLoader.ILoadCallback<FollowUpdateResult>() {
            @Override
            public void onCacheComplete(FollowUpdateResult result) {
 
            }
 
            @Override
            public void onHttpComplete(int resultCode, FollowUpdateResult result) {
                if (resultCode != IBeanLoader.LOAD_SUCCESS) {
                    return;
                }
                updateFollowCallback.updateFollow(resultCode, result);
            }
 
            @Override
            public void onContentChange() {
 
            }
        });
    }

2)PersonalDetailFragment.java
 

  UserUtil.updateFollow(getContext(), dest_user_id, false, new UserUtil.UpdateFollowCallback() {
                        @Override
                        public void updateFollow(int resultCode, FollowUpdateResult result) {
                            if (!isFinishing()) {
                                if (resultCode != IBeanLoader.LOAD_SUCCESS) {
                                } else {
                                    if (result.code == 0) {
                                        focus_or_not = 1;
                                        getActivity().invalidateOptionsMenu();
                                    }
                                }
                            }
                        }
                    });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值