refactor(architecture): 重构应用启动和导航逻辑
- 修改 ReactHostHelper 和 MultipleReactActivityDelegate以支持新的架构 - 更新 AppManager 和 NavigationManager 以优化活动管理和导航 - 重构 MainActivity 和 MainApplication 以使用 ReactActivity 和 ReactNativeHost - 更新资源文件和 TypeScript 文件以适应新的结构
这个提交包含在:
父节点
205e4e8680
当前提交
da112f0733
@ -12,6 +12,7 @@ class ReactHostHelper(
|
||||
task.waitForCompletion()
|
||||
return task.getResult()
|
||||
}
|
||||
|
||||
fun getOrCreateReactInstance() {
|
||||
delegate.isInstanceInitialized
|
||||
}
|
||||
|
||||
@ -13,16 +13,20 @@ object AppManager {
|
||||
activityStack.remove(activity)
|
||||
}
|
||||
|
||||
fun finishActivity(activity: Activity?) {
|
||||
fun finishActivity(activity: Activity?): Activity? {
|
||||
if (activity != null) {
|
||||
removeActivity(activity)
|
||||
activity.finish()
|
||||
return activity
|
||||
} else {
|
||||
lastActivity()?.apply {
|
||||
removeActivity(this)
|
||||
this.finish()
|
||||
return this
|
||||
}
|
||||
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun lastActivity(): Activity? {
|
||||
|
||||
@ -34,12 +34,10 @@ class BuzActivity : ReactActivity() {
|
||||
AppManager.addActivity(this)
|
||||
Toast.makeText(this, "BuzActivity:" + NavigationHelper.routerName, Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
// if (reactHost.lifecycleState != LifecycleState.RESUMED) {
|
||||
// reactHost.onHostResume(this)
|
||||
// }
|
||||
// Handler(Looper.getMainLooper()).post {
|
||||
// reactHost.reload("Requested by CodeUpdater")
|
||||
// }
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
@ -1,15 +1,18 @@
|
||||
package com.trust.ywx
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.trust.ywx.specs.navigation.NavigationHelper
|
||||
import com.facebook.react.ReactActivity
|
||||
import com.facebook.react.ReactActivityDelegate
|
||||
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
|
||||
import com.facebook.react.defaults.DefaultReactActivityDelegate
|
||||
|
||||
class MainActivity : ReactActivity() {
|
||||
override fun getMainComponentName(): String = "app"
|
||||
override fun createReactActivityDelegate(): ReactActivityDelegate =
|
||||
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
NavigationHelper.routerName = "app"
|
||||
startActivity(Intent(this, BuzActivity::class.java))
|
||||
finish()
|
||||
AppManager.addActivity(this)
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,10 +15,8 @@ import java.io.File
|
||||
|
||||
class MainApplication : Application(), ReactApplication {
|
||||
|
||||
override val reactNativeHost: ReactNativeHost =
|
||||
object : DefaultReactNativeHost(this) {
|
||||
override fun getPackages(): List<ReactPackage> =
|
||||
PackageList(this).packages.apply {
|
||||
override val reactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) {
|
||||
override fun getPackages(): List<ReactPackage> = PackageList(this).packages.apply {
|
||||
// Packages that cannot be autolinked yet can be added manually here, for example:
|
||||
add(NavigationPackage())
|
||||
}
|
||||
@ -29,12 +27,6 @@ class MainApplication : Application(), ReactApplication {
|
||||
override fun getBundleAssetName(): String? =
|
||||
if (getUseDeveloperSupport()) "index.android.bundle" else null
|
||||
|
||||
//
|
||||
// override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
|
||||
// super.getJSBundleFile()
|
||||
// } else {
|
||||
// FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
|
||||
// }
|
||||
override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
|
||||
null
|
||||
} else {
|
||||
|
||||
@ -38,6 +38,7 @@ class MultipleReactActivityDelegate(
|
||||
}
|
||||
|
||||
if (enableBridgelessArchitecture()) {
|
||||
|
||||
this.mReactDelegate =
|
||||
ReactDelegate(
|
||||
this.plainActivity,
|
||||
@ -59,6 +60,7 @@ class MultipleReactActivityDelegate(
|
||||
)
|
||||
|
||||
this.loadApp(mainComponentName)
|
||||
|
||||
} else {
|
||||
this.mReactDelegate =
|
||||
object : ReactDelegate(
|
||||
@ -86,9 +88,14 @@ class MultipleReactActivityDelegate(
|
||||
|
||||
val instance =
|
||||
reactNativeHost.reactInstanceManager.currentReactContext?.catalystInstance
|
||||
instance?.loadScriptFromAssets(
|
||||
context.assets,
|
||||
"assets://index.android.bundle",
|
||||
val fileName = FileHelper.getFilePath(
|
||||
"$mainComponentName.android.bundle",
|
||||
reactActivity.applicationContext,
|
||||
"bundles"
|
||||
)
|
||||
instance?.loadScriptFromFile(
|
||||
fileName,
|
||||
fileName,
|
||||
false
|
||||
)
|
||||
Log.i("TestApp", "loaded biz bundle")
|
||||
@ -185,4 +192,5 @@ class MultipleReactActivityDelegate(
|
||||
* context will no longer be valid.
|
||||
*/
|
||||
override fun getCurrentReactContext(): ReactContext = mReactDelegate!!.currentReactContext!!
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package com.trust.ywx.specs.navigation
|
||||
|
||||
import android.content.Intent
|
||||
import android.widget.Toast
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.trust.ywx.AppManager
|
||||
import com.trust.ywx.BuzActivity
|
||||
@ -12,15 +13,14 @@ class NavigationManager(reactContext: ReactApplicationContext) :
|
||||
NavigationHelper.routerName = name
|
||||
AppManager.lastActivity()
|
||||
?.apply {
|
||||
Toast.makeText(this, "Navigate to $name", Toast.LENGTH_SHORT).show()
|
||||
startActivity(Intent(this, BuzActivity::class.java))
|
||||
overridePendingTransition(0, 0)
|
||||
}
|
||||
// AppManager.lastActivity()
|
||||
// ?.overridePendingTransition(0, 0)
|
||||
}
|
||||
|
||||
override fun pop() {
|
||||
AppManager.finishActivity(null)
|
||||
AppManager.finishActivity(null)?.overridePendingTransition(0, 0)
|
||||
}
|
||||
|
||||
override fun getName() = NAME
|
||||
|
||||
@ -18,8 +18,8 @@ class FileHelper {
|
||||
}
|
||||
|
||||
fun getFilePath(name: String, context: Context?, type: String?): String {
|
||||
val path = getDirPath(context, type) + File.separator + name
|
||||
return path
|
||||
val path = getDirPath(context, null)
|
||||
return if (type.isNullOrBlank()) path + File.separator + name else path + File.separator + type + File.separator + name
|
||||
}
|
||||
|
||||
fun copyAssetFileToInternalStorage(
|
||||
|
||||
@ -6,7 +6,13 @@
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="button"
|
||||
android:text="button1"
|
||||
android:id="@+id/btn1"
|
||||
android:layout_gravity="center"/>
|
||||
<Button
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="button2"
|
||||
android:id="@+id/btn2"
|
||||
android:layout_gravity="center"/>
|
||||
</LinearLayout>
|
||||
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
@ -15,7 +15,7 @@ import {
|
||||
import { showMessage } from '@common/ToastHelper.ts';
|
||||
import Toast from 'react-native-toast-message';
|
||||
|
||||
function App() {
|
||||
function YWQ() {
|
||||
const isDarkMode = useColorScheme() === 'dark';
|
||||
|
||||
return (
|
||||
@ -41,4 +41,4 @@ const styles = StyleSheet.create({
|
||||
},
|
||||
});
|
||||
|
||||
export default App;
|
||||
export default YWQ;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { AppRegistry } from 'react-native';
|
||||
import App from './Ywq.tsx';
|
||||
import YWQ from '@ywx/Ywq.tsx';
|
||||
import { Apps } from '@common/NavigationHelper.ts';
|
||||
|
||||
AppRegistry.registerComponent(Apps.Ywq, () => App);
|
||||
AppRegistry.registerComponent(Apps.Ywq, () => YWQ);
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户