Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

screen model doesn't survive the configuration changes #62

Closed
Anton111111 opened this issue May 3, 2022 · 11 comments · Fixed by #84
Closed

screen model doesn't survive the configuration changes #62

Anton111111 opened this issue May 3, 2022 · 11 comments · Fixed by #84

Comments

@Anton111111
Copy link

I try use Screen + ScreenModel + Hilt.

I create Screen like this:

object SearchScreen : Screen {

    @Composable
    override fun Content() {
        val screenModel = getScreenModel<SearchScreenModel>()
        Log.e("!!!!","!!!!Use screenmodel: $screenModel")
        SearchScreen(screenModel)
    }
}

And my ScreenModel:

class SearchScreenModel @Inject constructor(
    private val searchUseCase: SearchUseCase
) : ScreenModel {
   //Some code
}

But when i rotate my phone screen model recreated. Shouldn't the screen model survive the configuration changes? OR am I doing something wrong?

@Anton111111
Copy link
Author

I think Hilt has nothing to do with it. I tried to do it without Hilt and still the ScreenModel is recreated. Maybe I'm not understanding correctly and it should be recreated when the phone rotates?

@Anton111111
Copy link
Author

I'm completely bewildered right now. I run the sample application and it works as it should. I completely copy the Activity, Screen, ScreenModel code in my app (where I want to migrate to voyager) and the screenmodel starts recreating when I flip the phone. Can anyone help me ?

@Syer10
Copy link
Contributor

Syer10 commented May 4, 2022

Try this, object SearchScreen : Screen should be class SearchScreen : AndroidScreen() and you should use remember { SearchScreen() } if this is your initial screen in the navigator. I cant tell anything else without looking at your code.

I only use object screens when there will be no view/screen models, and when using a regular Screen you should set the id like override val key: ScreenKey = uniqueScreenKey, AndroidScreen's do this automatically.

@Anton111111
Copy link
Author

Try this, object SearchScreen : Screen should be class SearchScreen : AndroidScreen() and you should use remember { SearchScreen() } if this is your initial screen in the navigator. I cant tell anything else without looking at your code.

Where i need to use remember { SearchScreen() } ?
I look at sample:
https://github.com/adrielcafe/voyager/blob/main/samples/android/src/main/java/cafe/adriel/voyager/sample/hiltIntegration/HiltMainActivity.kt

It started with Navigator(HiltListScreen())

@Syer10
Copy link
Contributor

Syer10 commented May 4, 2022

Something like Navigator(remember { HiltListScreen() })

Its a safeguard because Compose can re-create classes based on parent composables.

@Anton111111
Copy link
Author

Something like Navigator(remember { HiltListScreen() })

Here is cleaned project to show my problem:
https://github.com/Anton111111/VoyagerPlayground

Here is Activity:
https://github.com/Anton111111/VoyagerPlayground/blob/main/mobile/src/main/java/com/anton111111/anorak/ui/app/AppActivity.kt

Here is Screen:
https://github.com/Anton111111/VoyagerPlayground/blob/main/feature/movies/ui/src/main/java/com/anton111111/anorak/movies/ui/search/HiltListScreen.kt

When i rotate phone i see that screen is not recreated. But screen model is recreated.

@Anton111111
Copy link
Author

Something like Navigator(remember { HiltListScreen() })

I tried with remember. Same result ;(

@Syer10
Copy link
Contributor

Syer10 commented May 4, 2022

Ok I did some testing and this bug seems to be related to the changes done in 1.0.0-rc01, it doesnt happen in 1.0.0-beta16. So I guess use 1.0.0-beta16 for now until its fixed

@Anton111111
Copy link
Author

So I guess use 1.0.0-beta16 for now until its fixed

You are right! Thanks! On 1.0.0-beta16 it works good.

My problem is i started sample app from main branch where issue solved already.

@adrielcafe
Copy link
Owner

I've commented on #66 about a refactoring I'm doing that should solve all Android related issues (including configuration changes). It may take a while but will be worth it.

@beyondeye
Copy link

@Anton111111
Hi , I have just published a fork of voyager with bug fixes and new features here.
I will be happy to have your feedback.
This specific issue has been solved in my fork.

jobobby04 pushed a commit to jobobby04/TachiyomiSY that referenced this issue Oct 18, 2022
* Settings: M3 and two pane ui

* TrackingLoginDialog: Move close button

* Use small top bar

* Revert "Update voyager to v1.0.0-rc02"

This reverts commit 570fec6.

adrielcafe/voyager#62
(cherry picked from commit 5c5468f)

# Conflicts:
#	app/src/main/java/eu/kanade/presentation/manga/MangaScreen.kt
#	app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsMainScreen.kt
#	app/src/main/java/eu/kanade/presentation/more/settings/widget/TextPreferenceWidget.kt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants