Office Hours Transcript: 2021-10-26

Tad joined

hello, Tad!

Hi Mark!

how can I help you today?

So, I am seeing some odd behavior and am wondering if the Nav components might somehow be responsible.

 

When I Navigate away from 'MainActivity' to a different activity using a Nav graph, then come back, the bundle passed to onCreate is always null.

 

So my onSaveInstanceState values are lost.

 

I have verified that method indeed gets called, so there is state being saved

 

but onRestoreInstanceState never gets called, and the bundle in onCreate is always null.

I have not used Jetpack Navigation to navigate between activities. The implication is that the old activity is being finished and a new activity instance is being created.

Which tells me that 'MainActivity' is not being 'restored’, it is being created new each time I return from the called activity?

that’s what it sounds like

 

you could confirm this by seeing if onDestroy() is called in your original activity instance

So what is the recommended approach if using Navigation components to save state? Shared Preferences?

again, I have not used Jetpack Navigation to navigate between activities, only between fragments and composables within a single activity

 

so, I do not know what the expected behavior is, nor what recommended workarounds might be

OK - I haven’t gotten around to re-architecting using a single activity and the rest fragments, I suppose that’s next…

I do not get the sense that using Navigation for activities is all that common, so perhaps the best answer is to hold off on using Navigation until you start switching to be more fragment-centric

Well too late - I changed to Navigation when it first came out, and at that time there was nothing about this problem mentioned. 'One activity many fragments' came later. :) What have you heard on updates to the cross-platform Kotlin vs. Flutter?

 

i.e. maturity, robustness, etc.

that depends on whether you want cross-platform UI or not

I do.

Kotlin/Multiplatform is doing pretty well, and more libraries are being reworked to support it

 

however, Compose for Stuff Other Than Android is still very much a work in progress, and there’s no sign of Compose for iOS

Btw - interestingly - navigating away from MainActivity didn’t destroy right away. As soon as I pressed the back button from my called activity, it was in fact called.

OK, that’s strange

Verified with another activity from my main screen.

are you doing anything unusual with your MainActivity in terms of flags and stuff on the <activity> element in the manifest, like using android:launchMode?

No

 
    <activity
        android:name=".ui.MainActivity"
        android:label="@string/app_name_full"
        android:theme="@style/AppTheme.NoActionBar"> <!-- Set main activity to use abbreviated name -->
    </activity>

yeah, that’s pretty boring 😁

 

are you doing anything unusual in terms of managing BACK operations, like registering your own back handler or something?

MainActivity isn’t the launcher, I have a SplashActivity that is, which shows a splash screen, does some init stuff, then launches MainActivity

 

Not registering any back handler

you might try creating a scrap two-activity app, using navigation to go between them like you are doing in your real app, and see if you have the same problem

Using Nav for all of that, and it is a flat hierarchy - just calls 5 other activities, and the user presses back to get back to MainActivitgy

if you see the same problem in the scrap app, then presumably this is either a library bug or intended-but-unexpected behavior, in which case you might file an issue on the issue tracker to get clarification

yeah

if you do not see the same problem in the scrap app, then there is something else about your real app that is triggering this behavior, and adding more stuff to the scrap app might uncover what it is

It took 15 months but I got my permissions persistence bug kind of addressed, they upped the limit from 128 to 512 woohoo!

it’s progress!

For the couple of states (int values) I ended up making them static variables in MainActivity, which admittedly is a hack but will do for now until I go back and change all that out.

 

Gonna go now, thanks for the info…

sorry I didn’t have a simpler solution for you!

Tad left