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();
}
}
}
}
});