状态接上一章

1.状态恢复

parceable

状态接上一章_第1张图片

状态接上一章_第2张图片

pluginManagement {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }

    //插件
    plugins{
        id("com.android.application")version("7.1.0-alpha05")
        id("com.android.library")version("7.1.0-alpha05")
        id("org.jetbrains.kotlin.android")version("1.5.10")
        id("org.jetbrains.kotlin.plugin.parcelize")version("1.5.10")
    }


}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "JetpackComposeState"
include(":app")
 
package com.jmj.jetpackcomposestate.examples

import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.versionedparcelable.ParcelField


import com.jmj.jetpackcomposestate.ui.theme.JetpackComposeStateTheme
import kotlinx.android.parcel.Parcelize

class StateRecoveryParcelableActivity :ComponentActivity(){

    @Parcelize //自动 实现这个接口
    data class City(val name:String , var country:String) : Parcelable

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            JetpackComposeStateTheme {
                CityScreen()
            }
        }

    }


    @Composable
    fun CityScreen(){

        val (city,setCity) = rememberSaveable {
            mutableStateOf(City("Madrid","Spain"))
        }

        Row (
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(10.dp)
        ){
            TextButton(onClick = {setCity(City("Beijing","China")) },
                colors = ButtonDefaults.buttonColors()
                ) {
                Text(text = "Click to change")
            }
            
            Text(text = "${city.country} - ${city.name}")
        }
    }
}

状态接上一章_第3张图片

MapSaver

 

package com.jmj.jetpackcomposestate.examples

import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.mapSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.versionedparcelable.ParcelField


import com.jmj.jetpackcomposestate.ui.theme.JetpackComposeStateTheme
import kotlinx.android.parcel.Parcelize


class StateRecoveryMapActivity :ComponentActivity(){

    data class City(val name:String , var country:String)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            JetpackComposeStateTheme {
                CityScreen()
            }
        }

    }


    @Composable
    fun CityScreen(){
        //指定 mapSaver , 用于存储时如何构建一个mao对象,获取时如何构建一个City对象
        val citySaver = run {
            val nameKey = "Name"
            val  countryKey = "Country"
            mapSaver(
                save = { mapOf(nameKey to it.name,countryKey to it.country) },
                restore = { City(it[nameKey] as String,it[countryKey] as String)}
            )
        }



        val (city,setCity) = rememberSaveable(stateSaver = citySaver) {
            mutableStateOf(City("Madrid","Spain"))
        }

        Row (
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(10.dp)
        ){
            TextButton(onClick = {setCity(City("Beijing","China")) },
                colors = ButtonDefaults.buttonColors()
                ) {
                Text(text = "Click to change")
            }
            
            Text(text = "${city.country} - ${city.name}")
        }
    }
}

状态接上一章_第4张图片

ListSaver 

package com.jmj.jetpackcomposestate.examples

import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.mapSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.versionedparcelable.ParcelField


import com.jmj.jetpackcomposestate.ui.theme.JetpackComposeStateTheme
import kotlinx.android.parcel.Parcelize


class StateRecoveryListActivity :ComponentActivity(){

    data class City(val name:String , var country:String)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            JetpackComposeStateTheme {
                CityScreen()
            }
        }

    }


    @Composable
    fun CityScreen(){
        //指定 mapSaver , 用于存储时如何构建一个map对象,获取时如何构建一个City对象
//        val nameKey = "Name"
//        val  countryKey = "Country"
//           val citySaver = mapSaver(
//                save = { mapOf(nameKey to it.name,countryKey to it.country) },
//                restore = { City(it[nameKey] as String,it[countryKey] as String)}
//            )


        val citySaver = listSaver(
            save = { listOf(it.name,it.country) },
            restore = { City(it[0] as String,it[1] as String)}
        )



        val (city,setCity) = rememberSaveable(stateSaver = citySaver) {
            mutableStateOf(City("Madrid","Spain"))
        }

        Row (
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(10.dp)
        ){
            TextButton(onClick = {setCity(City("Beijing","China")) },
                colors = ButtonDefaults.buttonColors()
                ) {
                Text(text = "Click to change")
            }
            
            Text(text = "${city.country} - ${city.name}")
        }
    }
}

状态接上一章_第5张图片

你可能感兴趣的:(Androd,Jetpack,Compose,android)