react native RN 安卓原生代码优化部分

技术分享 2019-10-01 00:00:00
react native RN 安卓原生代码部分整体比较粗糙
我认为达到能用的地步起码要改的代码有

①支持开屏页面,不是欢迎页,而是加载时候的开屏页面


②如果后台已经存在当前app点击桌面图标,不应该重启app


③安卓返回键最好能把app退出到后台,而不会直接kill掉


主要改 MainActivity.java 参考:

public class MainActivity extends ReactActivity {

/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "Mshop";
}



//退出隐藏到后台
@Override
public void invokeDefaultOnBackPressed() {
// do not call super.invokeDefaultOnBackPressed() as it will close the app. Instead lets just put it in the background.
moveTaskToBack(true);
}



@Override
protected void onCreate(Bundle savedInstanceState) {
//如果在后台桌面点击不重启
if (!this.isTaskRoot()) {
Intent mainIntent = getIntent();
String action = mainIntent.getAction();
if (mainIntent.hasCategory(mainIntent.CATEGORY_LAUNCHER) && mainIntent.ACTION_MAIN.equals(action)) {
finish();
return;
}
}


SplashScreen.show(this,true); // true means full screen

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); //禁止截图


super.onCreate(savedInstanceState);

}


}
咨询小瓶科技
咨询我们
顶部