Android – onbackpressed fragments do not work when implementing interfaces

I tried to override the onbackpressed method in the fragment. But it gave me a syntax error. It should override the super method in the interface. Why? There are other methods in my fragment class, such as ondestroy, but there are no errors. Why separate back pressure? I also tried onkeydown. The same error. Paste it under my code

  public TestClass extends Fragment implements    Testinterface
{  @Override //error must override or implement    supertype method
 public void onBackPressed ()
{
 if (check)
Do somethin
 else
  getActivity().finish ()
  //super.onBackPressed () // error here if I use this
  }

resolvent:

You must implement the fragment in the key and check the key code. The onbackpress () method can be used in the activity - it is the logical parent of the fragment

Try this:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>