现在我们开发一般大多用的导航框架 是 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方法 写上如上代码 就完美解决了。