本文共 4260 字,大约阅读时间需要 14 分钟。
使用 React Native开发,iOS搞完,开始适配安卓,由于木有接触过安卓,所以碰到了很多问题,第一个问题,安卓的返回键BackAndroid问题, 我写了一个工具类,来搞定,其中用到了原生代码与js交互;好吧,上代码:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 'use strict';
- import React,{
- Platform,
- Navigator,
- BackAndroid,
- ToastAndroid,
- NativeModules,
- } from 'react-native';
-
-
- var NativeCommonTools = NativeModules.CommonTools;
-
- export default {
-
- addBackAndroidListener(navigator) {
- if (Platform.OS === 'android') {
- BackAndroid.addEventListener('hardwareBackPress',() => {
- return this.onBackAndroid(navigator);
- });
- }
- },
-
-
- removeBackAndroidListener() {
- if (Platform.OS === 'android') {
- BackAndroid.removeEventListener('hardwareBackPress', () => {
- });
- }
- },
-
-
- onBackAndroid(navigator) {
- if (!navigator) return false;
- const routers = navigator.getCurrentRoutes();
-
- if (routers.length > 1) {
- const top = routers[routers.length - 1];
- if (top.ignoreBack || top.component.ignoreBack) {
-
- return true;
- }
- const handleBack = top.handleBack || top.component.handleBack;
- if (handleBack) {
-
- return handleBack();
- }
-
- navigator.pop();
- return true;
- }
-
- if (this.lastBackPressed && (this.lastBackPressed + 2000 >= Date.now())) {
-
- NativeCommonTools.onBackPressed();
- return true;
- }
- this.lastBackPressed = Date.now();
- ToastAndroid.show('再按一次退出应用',ToastAndroid.SHORT);
- return true;
- },
-
-
- customHandleBack(navigator, handleBack) {
- if (navigator) {
- let routes = navigator.getCurrentRoutes();
- let lastRoute = routes[routes.length - 1];
- lastRoute.handleBack = handleBack;
- }
- },
-
- }
其中的java原生代码如下:
管理类:RCTCommonToolsPackage (ps:如是不明白,可以去这里 学习:);
- package com.commonTools;
-
- import com.facebook.react.ReactPackage;
- import com.facebook.react.bridge.JavaScriptModule;
- import com.facebook.react.bridge.NativeModule;
- import com.facebook.react.bridge.ReactApplicationContext;
- import com.facebook.react.uimanager.ViewManager;
-
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
-
- public class RCTCommonToolsPackage implements ReactPackage {
- @Override
- public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
- return Arrays.<NativeModule>asList(new RCTCommonTools(reactContext));
- }
-
- @Override
- public List<Class<? extends JavaScriptModule>> createJSModules() {
- return Collections.emptyList();
- }
-
- @Override
- public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
- return Collections.emptyList();
- }
- }
自定义方法的类:RCTCommonTools
- package com.commonTools;
-
- import android.content.Intent;
-
- import com.facebook.react.bridge.Callback;
- import com.facebook.react.bridge.ReactApplicationContext;
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
- import com.facebook.react.bridge.ReactMethod;
-
- import com.tcpaydls.BuildConfig;
-
- public class RCTCommonTools extends ReactContextBaseJavaModule {
-
- public RCTCommonTools(ReactApplicationContext reactContext) {
- super(reactContext);
- }
-
- @Override
- public String getName() {
- return "RCTCommonTools";
- }
-
-
-
-
- @ReactMethod
- public void onBackPressed() {
- Intent setIntent = new Intent(Intent.ACTION_MAIN);
- setIntent.addCategory(Intent.CATEGORY_HOME);
- setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- getCurrentActivity().startActivity(setIntent);
-
- }
- }
转载地址:http://mueni.baihongyu.com/