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,6 +50,12 @@ class MainActivity : NodeComponentActivity() {
appBindings.matrixClientsHolder().restore(savedInstanceState) appBindings.matrixClientsHolder().restore(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
setContent { setContent {
MainContent(appBindings)
}
}
@Composable
private fun MainContent(appBindings: AppBindings) {
ElementTheme { ElementTheme {
Box( Box(
modifier = Modifier modifier = Modifier
@ -62,6 +69,7 @@ class MainActivity : NodeComponentActivity() {
plugins = listOf( plugins = listOf(
object : NodeReadyObserver<MainNode> { object : NodeReadyObserver<MainNode> {
override fun init(node: MainNode) { override fun init(node: MainNode) {
Timber.tag(loggerTag.value).w("onMainNodeInit")
mainNode = node mainNode = node
mainNode.handleIntent(intent) mainNode.handleIntent(intent)
} }
@ -72,7 +80,6 @@ class MainActivity : NodeComponentActivity() {
} }
} }
} }
}
/** /**
* Called when: * Called when:
@ -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.
// 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) mainNode.handleIntent(intent)
} else {
setIntent(intent)
}
} }
override fun onPause() { override fun onPause() {