Fix crash on MainActivity when restored

This commit is contained in:
ganfra 2023-06-09 22:43:59 +02:00
parent e591a588b3
commit e3d939726c

View file

@ -23,6 +23,7 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
@ -39,7 +40,7 @@ private val loggerTag = LoggerTag("MainActivity")
class MainActivity : NodeComponentActivity() { class MainActivity : NodeComponentActivity() {
lateinit var mainNode: MainNode private lateinit var mainNode: MainNode
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
Timber.tag(loggerTag.value).w("onCreate, with savedInstanceState: ${savedInstanceState != null}") Timber.tag(loggerTag.value).w("onCreate, with savedInstanceState: ${savedInstanceState != null}")
@ -49,26 +50,32 @@ class MainActivity : NodeComponentActivity() {
appBindings.matrixClientsHolder().restore(savedInstanceState) appBindings.matrixClientsHolder().restore(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
setContent { setContent {
ElementTheme { MainContent(appBindings)
Box( }
modifier = Modifier }
.fillMaxSize()
.background(MaterialTheme.colorScheme.background), @Composable
) { private fun MainContent(appBindings: AppBindings) {
NodeHost(integrationPoint = appyxIntegrationPoint) { ElementTheme {
MainNode( Box(
it, modifier = Modifier
appBindings.mainDaggerComponentOwner(), .fillMaxSize()
plugins = listOf( .background(MaterialTheme.colorScheme.background),
object : NodeReadyObserver<MainNode> { ) {
override fun init(node: MainNode) { NodeHost(integrationPoint = appyxIntegrationPoint) {
mainNode = node MainNode(
mainNode.handleIntent(intent) it,
} appBindings.mainDaggerComponentOwner(),
plugins = listOf(
object : NodeReadyObserver<MainNode> {
override fun init(node: MainNode) {
Timber.tag(loggerTag.value).w("onMainNodeInit")
mainNode = node
mainNode.handleIntent(intent)
} }
) }
) )
} )
} }
} }
} }
@ -80,11 +87,17 @@ class MainActivity : NodeComponentActivity() {
* - a notification is clicked. * - a notification is clicked.
* - the app is going to background (<- this is strange) * - the app is going to background (<- this is strange)
*/ */
override fun onNewIntent(intent: Intent?) { override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent) super.onNewIntent(intent)
Timber.tag(loggerTag.value).w("onNewIntent") Timber.tag(loggerTag.value).w("onNewIntent")
intent ?: return // If the mainNode is not init yet, keep the intent for later.
mainNode.handleIntent(intent) // It can happen when the activity is killed by the system. The methods are called in this order :
// onCreate(savedInstanceState=true) -> onNewIntent -> onResume -> onMainNodeInit
if (::mainNode.isInitialized) {
mainNode.handleIntent(intent)
} else {
setIntent(intent)
}
} }
override fun onPause() { override fun onPause() {