refactor(architecture): 重构应用启动和导航逻辑

- 修改 ReactHostHelper 和 MultipleReactActivityDelegate以支持新的架构
- 更新 AppManager 和 NavigationManager 以优化活动管理和导航
- 重构 MainActivity 和 MainApplication 以使用 ReactActivity 和 ReactNativeHost
- 更新资源文件和 TypeScript 文件以适应新的结构
这个提交包含在:
xuqm 2025-07-18 11:25:27 +08:00
父节点 205e4e8680
当前提交 da112f0733
共有 14 个文件被更改,包括 627 次插入52088 次删除

查看文件

@ -12,6 +12,7 @@ class ReactHostHelper(
task.waitForCompletion() task.waitForCompletion()
return task.getResult() return task.getResult()
} }
fun getOrCreateReactInstance() { fun getOrCreateReactInstance() {
delegate.isInstanceInitialized delegate.isInstanceInitialized
} }

查看文件

@ -13,16 +13,20 @@ object AppManager {
activityStack.remove(activity) activityStack.remove(activity)
} }
fun finishActivity(activity: Activity?) { fun finishActivity(activity: Activity?): Activity? {
if (activity != null) { if (activity != null) {
removeActivity(activity) removeActivity(activity)
activity.finish() activity.finish()
return activity
} else { } else {
lastActivity()?.apply { lastActivity()?.apply {
removeActivity(this) removeActivity(this)
this.finish() this.finish()
return this
} }
} }
return null
} }
fun lastActivity(): Activity? { fun lastActivity(): Activity? {

查看文件

@ -34,12 +34,10 @@ class BuzActivity : ReactActivity() {
AppManager.addActivity(this) AppManager.addActivity(this)
Toast.makeText(this, "BuzActivity:" + NavigationHelper.routerName, Toast.LENGTH_SHORT) Toast.makeText(this, "BuzActivity:" + NavigationHelper.routerName, Toast.LENGTH_SHORT)
.show() .show()
// if (reactHost.lifecycleState != LifecycleState.RESUMED) { }
// reactHost.onHostResume(this)
// } override fun onResume() {
// Handler(Looper.getMainLooper()).post { super.onResume()
// reactHost.reload("Requested by CodeUpdater")
// }
} }
override fun onDestroy() { override fun onDestroy() {

查看文件

@ -1,15 +1,18 @@
package com.trust.ywx package com.trust.ywx
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import com.facebook.react.ReactActivity
import com.trust.ywx.specs.navigation.NavigationHelper 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?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
NavigationHelper.routerName = "app" AppManager.addActivity(this)
startActivity(Intent(this, BuzActivity::class.java))
finish()
} }
} }

查看文件

@ -15,38 +15,30 @@ import java.io.File
class MainApplication : Application(), ReactApplication { class MainApplication : Application(), ReactApplication {
override val reactNativeHost: ReactNativeHost = override val reactNativeHost: ReactNativeHost = object : DefaultReactNativeHost(this) {
object : DefaultReactNativeHost(this) { override fun getPackages(): List<ReactPackage> = PackageList(this).packages.apply {
override fun getPackages(): List<ReactPackage> = // Packages that cannot be autolinked yet can be added manually here, for example:
PackageList(this).packages.apply { add(NavigationPackage())
// Packages that cannot be autolinked yet can be added manually here, for example:
add(NavigationPackage())
}
override fun getJSMainModuleName(): String =
if (getUseDeveloperSupport()) "index" else "commom"
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 {
FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
}
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
override val isNewArchEnabled: Boolean = true
override val isHermesEnabled: Boolean = true
} }
override fun getJSMainModuleName(): String =
if (getUseDeveloperSupport()) "index" else "commom"
override fun getBundleAssetName(): String? =
if (getUseDeveloperSupport()) "index.android.bundle" else null
override fun getJSBundleFile(): String? = if (getUseDeveloperSupport()) {
null
} else {
FileHelper.getFilePath("common.android.bundle", this@MainApplication, "bundles")
}
override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
override val isNewArchEnabled: Boolean = true
override val isHermesEnabled: Boolean = true
}
override val reactHost: ReactHost override val reactHost: ReactHost
get() = getDefaultReactHost(applicationContext, reactNativeHost) get() = getDefaultReactHost(applicationContext, reactNativeHost)

查看文件

@ -38,6 +38,7 @@ class MultipleReactActivityDelegate(
} }
if (enableBridgelessArchitecture()) { if (enableBridgelessArchitecture()) {
this.mReactDelegate = this.mReactDelegate =
ReactDelegate( ReactDelegate(
this.plainActivity, this.plainActivity,
@ -59,6 +60,7 @@ class MultipleReactActivityDelegate(
) )
this.loadApp(mainComponentName) this.loadApp(mainComponentName)
} else { } else {
this.mReactDelegate = this.mReactDelegate =
object : ReactDelegate( object : ReactDelegate(
@ -86,9 +88,14 @@ class MultipleReactActivityDelegate(
val instance = val instance =
reactNativeHost.reactInstanceManager.currentReactContext?.catalystInstance reactNativeHost.reactInstanceManager.currentReactContext?.catalystInstance
instance?.loadScriptFromAssets( val fileName = FileHelper.getFilePath(
context.assets, "$mainComponentName.android.bundle",
"assets://index.android.bundle", reactActivity.applicationContext,
"bundles"
)
instance?.loadScriptFromFile(
fileName,
fileName,
false false
) )
Log.i("TestApp", "loaded biz bundle") Log.i("TestApp", "loaded biz bundle")
@ -185,4 +192,5 @@ class MultipleReactActivityDelegate(
* context will no longer be valid. * context will no longer be valid.
*/ */
override fun getCurrentReactContext(): ReactContext = mReactDelegate!!.currentReactContext!! override fun getCurrentReactContext(): ReactContext = mReactDelegate!!.currentReactContext!!
} }

查看文件

@ -1,6 +1,7 @@
package com.trust.ywx.specs.navigation package com.trust.ywx.specs.navigation
import android.content.Intent import android.content.Intent
import android.widget.Toast
import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactApplicationContext
import com.trust.ywx.AppManager import com.trust.ywx.AppManager
import com.trust.ywx.BuzActivity import com.trust.ywx.BuzActivity
@ -12,15 +13,14 @@ class NavigationManager(reactContext: ReactApplicationContext) :
NavigationHelper.routerName = name NavigationHelper.routerName = name
AppManager.lastActivity() AppManager.lastActivity()
?.apply { ?.apply {
Toast.makeText(this, "Navigate to $name", Toast.LENGTH_SHORT).show()
startActivity(Intent(this, BuzActivity::class.java)) startActivity(Intent(this, BuzActivity::class.java))
overridePendingTransition(0, 0) overridePendingTransition(0, 0)
} }
// AppManager.lastActivity()
// ?.overridePendingTransition(0, 0)
} }
override fun pop() { override fun pop() {
AppManager.finishActivity(null) AppManager.finishActivity(null)?.overridePendingTransition(0, 0)
} }
override fun getName() = NAME override fun getName() = NAME

查看文件

@ -18,8 +18,8 @@ class FileHelper {
} }
fun getFilePath(name: String, context: Context?, type: String?): String { fun getFilePath(name: String, context: Context?, type: String?): String {
val path = getDirPath(context, type) + File.separator + name val path = getDirPath(context, null)
return path return if (type.isNullOrBlank()) path + File.separator + name else path + File.separator + type + File.separator + name
} }
fun copyAssetFileToInternalStorage( fun copyAssetFileToInternalStorage(

查看文件

@ -6,7 +6,13 @@
<Button <Button
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="button" android:text="button1"
android:id="@+id/btn1" android:id="@+id/btn1"
android:layout_gravity="center"/> 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> </LinearLayout>

文件差异因一行或多行过长而隐藏

文件差异因一行或多行过长而隐藏

查看文件

@ -1,4 +1,4 @@
import 'react'; import 'react';
import 'react-native'; import 'react-native';
import '@common/NavigationHelper'; import '@common/NavigationHelper';
import '@common/ToastHelper.ts'; import '@common/ToastHelper.ts';

查看文件

@ -15,7 +15,7 @@ import {
import { showMessage } from '@common/ToastHelper.ts'; import { showMessage } from '@common/ToastHelper.ts';
import Toast from 'react-native-toast-message'; import Toast from 'react-native-toast-message';
function App() { function YWQ() {
const isDarkMode = useColorScheme() === 'dark'; const isDarkMode = useColorScheme() === 'dark';
return ( 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 { AppRegistry } from 'react-native';
import App from './Ywq.tsx'; import YWQ from '@ywx/Ywq.tsx';
import { Apps } from '@common/NavigationHelper.ts'; import { Apps } from '@common/NavigationHelper.ts';
AppRegistry.registerComponent(Apps.Ywq, () => App); AppRegistry.registerComponent(Apps.Ywq, () => YWQ);