Backwards compatibility of onBackPressed

本秂侑毒 提交于 2019-12-11 05:25:38

问题


If I use onBackPressed() on Android 1.5, my application crashes. Is there any possibility to deactivate this function if running on an Android 1.5 device?

The code there is not absolute necessary but a real "nice to have", so I would like to keep it on newer devices and just drop it on older ones.

Is this possible?

edit: I think I found it, just the old way:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
   if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
       // do something on back.
       return true;
   }

return super.onKeyDown(keyCode, event);
}

回答1:


You can try to detect runtime which version of SDK using your application and depending on that prepare different branches. Like:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event)  
{ 
    if(Build.VERSION.SDK_INT==Build.VERSION_CODES.CUPCAKE) //if it's 1.5
    {
       if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
       {        // do something on back.        
          return true;    
       }  
    }
    return super.onKeyDown(keyCode, event); 
} 


来源:https://stackoverflow.com/questions/4658021/backwards-compatibility-of-onbackpressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!