1

MainActivity contains five pages built using Compose. Navgation will repeatedly create destination, causing the screen to load repeatedly. I added Log to two places in ArticlePage.kt to print the log.The following is the log corresponding to each operation.

  1. The first generation of the application to open the log is

    D/ArticlePage: ArticlePage page is created

    D/ArticlePage: ArticlePage page is created

  2. Log after performing refresh operation

    I/ArticlePage: articles data change

    I/ArticlePage: articles data change

  3. Log when Navigation to other screen

    D/ArticlePage: ArticlePage page is created

    I/ArticlePage: articles data change

    D/ArticlePage: ArticlePage page is created

    I/ArticlePage: articles data change

  4. Log when switch to the ArticlePage again

    D/ArticlePage: ArticlePage page is created

    I/ArticlePage: articles data change

    D/ArticlePage: ArticlePage page is created

    I/ArticlePage: articles data change

    D/ArticlePage: ArticlePage page is created

    I/ArticlePage: articles data change

Because every time the log prints articles data change, it means that my screen needs to be refreshed once, and the animation in the screen will be loaded repeatedly. Very influential to use.How can I set it up to avoid page repeated loading.

MainActivity.kt

HomePages.forEachIndexed { index, screen ->
            selected = currentDestination == screen.route,
            onClick = {
                navController.navigate(screen.route) {
                        popUpTo(navController.graph.startDestinationId) {
                            saveState = true
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                }
            )
        }

MainActivityNavigation.kt

NavHost(navController, startDestination = HomePage.ArticlePage.route) {
        composable(HomePage.ArticlePage.route) {
            val viewModel:ArticleVM = hiltViewModel()
            ArticlePage(activity,viewModel) //The parameter activity is MainActivity
        }
    }

ArticlePage.kt

This is a mixed screen containing View and Compose.FragmentArticlesBinding contains a RecyclerView and refresh control widget

    @Composable
    fun ArticlePage(
        activity: MainActivity,
        articleVM: ArticleVM,
    ) {

        Log.d("ArticlePage", "ArticlePage page is created")

        AndroidViewBinding(factory = FragmentArticlesBinding::inflate) {
                ...... //init refresh

                // Directly monitor article data changes
                articleVM.articles.observe(activity) {
                    Log.i("ArticlePage", "articles data change")
                    ......... //other operations
                }
            }
    }

ArticleVM.kt

@HiltViewModel
class ArticleVM @Inject constructor() : BaseVM() {
    // article List declare
    val articles = MutableLiveData<ReturnList<ArticleDetail>>()
}
  • What do you mean by `combinable items`? – Abhimanyu Oct 07 '21 at 17:02
  • Use Compose to declare the interface – SakurajimaMaii Oct 08 '21 at 02:31
  • https://user-images.githubusercontent.com/46998172/136490478-8cc0e50c-0af0-4d62-89ad-f9b0542e1e1d.mp4 – SakurajimaMaii Oct 08 '21 at 02:50
  • I gave a video link, you can see that the animation on the interface is repeatedly started due to the repeated loading of the destination, which greatly affects the user experience. – SakurajimaMaii Oct 08 '21 at 02:51
  • 1
    Check out [this answer](https://stackoverflow.com/a/69491725/3585796). If it didn't help, please add your code with [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Phil Dukhov Oct 08 '21 at 07:08
  • Thank you very much for your reply. I read the link you gave but it doesn't seem to apply to my question. I re-edited the question and I would like to trouble you to read it again. – SakurajimaMaii Oct 08 '21 at 10:46
  • I am using compose navigation in my project and the only issue I have is that it is a bit weird how `lauchSingleTop` flag works (or it doesn't?). I find myself writing `popUpTo(...)` all the time. It looks like I have the same setup as you do with several tabs on the Home Screen, although each of my tab is contained in its own nested navigation graph and I do not use saveState / restoreState flags... What's your version of navigation? I'm on 2.4.0-alpha10, and I remember it did have bugs in previous iterations. – ntoskrnl Oct 09 '21 at 18:07
  • I used the same version as you – SakurajimaMaii Oct 10 '21 at 05:32
  • I refer to the official [Jetchat](https://github.com/android/compose-samples/tree/main/Jetchat) sample, and debug in the official sample and found that it also appears when I navigate between different destinations The problem of the destination being created repeatedly, it is mentioned in the official document about [NavHost](https://developer.android.com/jetpack/compose/navigation#create-navhost)'s explanation:As you navigate between composables, the content of the NavHost is automatically recomposed.I don't know if this is the reason why the destination is repeatedly created. – SakurajimaMaii Oct 10 '21 at 05:36
  • I have temporarily abandoned this part of the migration to Compose-Navigation, and for the time being, I still use the original ViewPager2+BottomNavigationBar solution – SakurajimaMaii Oct 10 '21 at 05:53

0 Answers0