我正在处理片段并遇到一个问题,当我试图回到我之前的片段后面的按钮按下我的应用程序关闭(编辑文本聚焦的情况)和同样的事情工作正常(编辑文本是没注意力。)
以下是我正在使用的代码: -
public static void OnBackPressHandler(View rootView,
final Fragment fragment, final String fragmentName,
final String fragmentTitle) {
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Session.ChangeFragment(fragment, fragmentName,
fragmentTitle);
return true;
}
}
return false;
}
});
}

以下是更改片段的代码: -
public void onFragmentChange(Fragment fragment, String fragmentName,
String headerTitle) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
setHeaderTitle(headerTitle);
if (sCheckFragment != null) {
fragmentManager.popBackStack();
sCheckFragment = fragmentName;
} else {
sCheckFragment = fragmentName;
}
fragmentTransaction.replace(R.id.frame_layout_main, fragment);
fragmentTransaction.addToBackStack(fragmentName);
fragmentTransaction.commitAllowingStateLoss();
}

我还尝试使用EditText.clearfocus()从编辑文本中删除焦点。但它对我没有帮助。
由于
答案 0 :(得分:1)
最后我可以通过界面使用它,我只需创建一个界面,当用户点击他的Android手机的Back-Button时,它将处理我的呼叫
这是一段代码:
我创建的界面: -
public interface OnBackPressHandler {
void ChangeFragmentOnBackPress(Fragment fragment, String fragmentName,
String headerTitle);
}
然后制作其Getter-Setter: -
private static OnBackPressHandler sBackPressHandler;
public static void ChangeFragmentOnBackPressHandler(Fragment fragment,
String fragmentName, String headerTitle) {
if (sBackPressHandler != null) {
sBackPressHandler.ChangeFragmentOnBackPress(fragment, fragmentName,
headerTitle);
}
}
public static void setBackPressHandler(
OnBackPressHandler listnerOnBackPressHandler) {
sBackPressHandler = listnerOnBackPressHandler;
}
public void ChangeFragmentOnBackPress(Fragment fragment,
String fragmentName, String headerTitle) {
mBackPressFragment = fragment;
mBackPressFragmentName = fragmentName;
mBackPressFragmentHeaderTitle = headerTitle;
mIsBackpressFirstTime = false;
}
然后我最后在主要活动calss的ONBackPress中调用它: -
public void onBackPressed() {
super.onBackPressed();
if (mIsBackpressFirstTime) {
finish();
} else {
if (mBackPressFragment != null) {
Session.ChangeFragment(mBackPressFragment,
mBackPressFragmentName, mBackPressFragmentHeaderTitle);
mBackPressFragment = null;
mBackPressFragmentName = "";
mBackPressFragmentHeaderTitle = "";
} else {
finish();
}
}
}
现在你需要从你的任何片段中简单地调用这个给定的方法: -
Session.ChangeFragmentOnBackPressHandler(
new HomeFragment(),
getActivity().getResources().getString(R.string.fragment_home),
getActivity().getResources().getString(
R.string.title_fragment_home));