@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
FragmentManager.BackStackEntry bSE = getSupportFragmentManager().getBackStackEntryAt(getSupportFragmentManager().getBackStackEntryCount() - 1);
Fragment frag = getSupportFragmentManager().findFragmentByTag(bSE.getName());
if (frag instanceof BackButtonBlocker)
{
BackButtonBlocker callback = (BackButtonBlocker) frag;
callback.onBackPressed();
}
else {
getSupportFragmentManager().popBackStack();
}
} else {
super.onBackPressed();
}
}
BackButtonBlockerインターフェイスはコールバック用です。私のフラグメントはBackButtonBlockerを実装しますが、時にはonBackPressedは発生しません(onKeyUpも同様です)。onBackPressedのアンドロイドアクティビティは呼び出されません。
ナビゲーション・ドロワーを開くと、onBackPressedと完全なロジックが正確に機能します。
なぜアクティビティにフォーカスが失われるのですか?
android.support.v4.app.Fragment
public interface BackButtonBlocker{ void onBackPressed();}
に
setFocusableInTouchMode(true); requestFocus();
を追加することで、この問題を修正しました。 – sofie戻るボタンを押すと、フラグメントでコールバックを取得する必要があります。 (たとえば、任意のダイアログを開くために) – sofie