Android Onbackpressed 在片段中实现接口时不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24075068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:46:00  来源:igfitidea点击:

Onbackpressed in fragment not working while implementing interface

androidandroid-fragments

提问by SjNerd

I am trying to override onbackpressed method inside fragment . But it gives me syntax error that it should override a super method in interface. Why so? I have other methods like ondestroy also in my fragment class but no error. Why for this backpressed alone. I tried onkeydown also. Same error. Pasting below my code.

我正在尝试覆盖 fragment 内的 onbackpressed 方法。但是它给了我语法错误,它应该覆盖接口中的超级方法。为什么这样?我的片段类中还有其他方法,例如 ondestroy,但没有错误。何必为此背道而驰。我也试过onkeydown。同样的错误。粘贴在我的代码下方。

  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
  }

回答by Skynet

You have to implement on key down in fragment, check for key code. The onBackPress() method can be used in an Activity -- which is the logical parent of your fragment.

您必须在片段中按键执行,检查键代码。onBackPress() 方法可以在 Activity 中使用——它是片段的逻辑父级。

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

回答by franmontiel

You can propagate onBackPressed() to all your fragments,for that you need to create two classes with the following methods and then make all your activties and fragments inherit from them:

您可以将 onBackPressed() 传播到所有片段,为此您需要使用以下方法创建两个类,然后使所有活动和片段都继承自它们:

public class BaseActivity extends ActionBarActivity {

    @Override
    public void onBackPressed() {
        boolean eventConsumed = false;
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                if (fragment instanceof BaseFragment) {
                    eventConsumed = eventConsumed
                            || ((BaseFragment) fragment).onBackPressed();
                }
            }
        }
        if (!eventConsumed) {
            super.onBackPressed();
        }
    }
}

public class BaseFragment extends Fragment {

    public boolean onBackPressed() {
        return false;
    }
}

Note that this code is using the support library, if you are not using it you need to do the appropriate changes.

请注意,此代码使用了支持库,如果您不使用它,则需要进行适当的更改。

回答by Daniel Park

Try to avoid using onKey in fragment. There is a better way. For maintenance, I recommend you to use getBackStackEntryCount()

尽量避免在片段中使用 onKey。有个更好的方法。对于维护,我建议您使用 getBackStackEntryCount()

in Activity

在活动中

final FragmentMananger fm = new getSupportFragmentManager(); 

@Override
protected void onCreate(Bundle b){
   super.onCreate(b);
   setContentView(R.layout.activity_main);

   Fragment fragment = new SomeFragment();
   // if you don't run on prior to Android 3.0 use getFragmentManager();
   fm.beginTransaction().replace(R.id.frame_container, fragment).addToBackStack(null).commit();
   // R.id.frame_container is the id of FrameLayout in activity_main.xml
}

@Override
public void onBackPressed(){
   if(fm.getBackStackEntryCount() == 0){
       finish();
   }else{
    super.onBackPressed();
   }
}