ReactNative 中 android按两次返回键退出当前应用程序
16lz
2021-01-26
现在我们开发一般大多用的导航框架 是 react-native-router-flux 和 react-navigation
这两个框架 很强大 但是在处理 android 按两次返回键退出当前应用程序时 确不尽如意。
终极万能适配解决方案
在原生MainActivity 中
public class MainActivity extends ReactActivity { /** * Returns the name of the main component registered from JavaScript. * This is used to schedule rendering of the component. */ private long firstTime = 0; @Override protected String getMainComponentName() { return "demo46"; } @Override protected void onCreate(Bundle savedInstanceState) {// RCTSplashScreen.openSplashScreen(this); //open splashscreen // RCTSplashScreen.openSplashScreen(this, true, ImageView.ScaleType.FIT_XY); //open splashscreen fullscreen SplashScreen.show(this); super.onCreate(savedInstanceState); GetSmsFromPhone.initActivity(this); NaviModule.initActivity(this); PhoneMailList.initActivity(this); //okhttp } @Override protected void onPause() { SplashScreen.hide(this); super.onPause(); } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); MoxieSDK.getInstance().clear(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { long secondTime = System.currentTimeMillis(); if (keyCode == KeyEvent.KEYCODE_BACK) { if (secondTime - firstTime < 2000) { System.exit(0); } else { Toast.makeText(getApplicationContext(), "再按一次退出应用程序", Toast.LENGTH_SHORT).show(); firstTime = System.currentTimeMillis(); } return true; } return super.onKeyDown(keyCode, event); }
重写onKeyDown方法 写上如上代码 就完美解决了。
更多相关文章
- android缓存框架ASimpleCache
- Android应用程序获取system权限
- Android获取桌面应用程序
- android 五大应用开发框架是什么
- Android单例关闭应用程序
- [Android] 开发资料收集:依赖注入框架
- android应用程序中获取view的位置(2)
- java android HTTP应用程序设计
- Android应用开发攻略