1

我想为我的主要活动中包含的所有片段实现自定义 onBackPressed 方法。但是当我的设备被按下时,我没有得到钩子。我试图在 SOF 中实现一些可用的东西,但它们都不能正常工作。请帮忙!

我在 onCreateView 中试过这个:

rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener( new OnKeyListener()
{
@Override
public boolean onKey( View v, int keyCode, KeyEvent event )
{
    if( keyCode == KeyEvent.KEYCODE_BACK)
    {
        return true;
    }
    return false;
}
} );
4

4 回答 4

2

使用下面的代码希望此代码对您有所帮助。

 rootView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {


            if (keyCode == KeyEvent.KEYCODE_BACK) {


                backFlag = backFlag++;

                if (backFlag == 2) {
                    getActivity().finish();
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                backFlag = 0;

                return true;
            }
            return false;
        }
    }); 
于 2015-09-19T15:03:49.820 回答
0

这里同样的问题。我通过将操作栏标题设置为片段来解决此问题,然后通过检查操作栏的标题检查 onBackPressed 哪个片段当前可见。它不是正确的解决方案,但它对我来说非常有效。如果你有任何问题告诉我。希望它对你有用。谢谢

于 2015-09-19T10:11:42.393 回答
0
public interface OnBackPressedListener {
  boolean onBackPressed();
}

public interface OnBackPressedNotifier {
  void registerOnBackPressedListener(OnBackPressedListener listener);
  void unregisterOnBackPressedListener(OnBackPressedListener listener);
}




public class SampleActivity extends Activity implements OnBackPressedNotifier {

  List<OnBackPressedListener> onBackPressedListeners = new ArrayList<>();

  @Override
  public void registerOnBackPressedListener(OnBackPressedListener listener)   {
    if (!onBackPressedListeners.contains(listener))
        onBackPressedListeners.add(listener);
  }

  @Override
  public void unregisterOnBackPressedListener(OnBackPressedListener listener) {
    if(onBackPressedListeners.contains(listener))
        onBackPressedListeners.remove(listener);
    }

  @Override
  protected void onDestroy() {
    onBackPressedListeners.clear();
    super.onDestroy();
  }

  private boolean notifyOnBackPressed(){
      boolean handledByFragment = false;
      for (OnBackPressedListener listener : onBackPressedListeners){
          handledByFragment = listener.onBackPressed();
          if (handledByFragment)
              break;
      }
      return handledByFragment;
  }

  @Override
  public void onBackPressed() {
      if (!notifyOnBackPressed())
          super.onBackPressed();
  }

}

public class SampleFragment extends android.support.v4.app.Fragment implements OnBackPressedListener {

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    ((OnBackPressedNotifier)getActivity()).registerOnBackPressedListener(this);
}

@Override
public void onDetach() {
    ((OnBackPressedNotifier)getActivity()).unregisterOnBackPressedListener(this);
    super.onDetach();
}

@Override
public boolean onBackPressed() {
    // Handle onBackPressed
    return false;
}
于 2015-09-19T14:47:01.797 回答
0

您可以使用新的 API。

activity?.onBackPressedDispatcher?.addCallback
于 2021-02-23T11:08:04.890 回答