Last active
January 17, 2023 12:13
-
-
Save alome007/465702a1b3cb75a191903eb1afda38d0 to your computer and use it in GitHub Desktop.
a counter viewmodel demonstrating how to use stateFlow in android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/mTxt" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="0" | |
android:textSize="32sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.498" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_bias="0.457" /> | |
<Button | |
android:id="@+id/incrementBtn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="48dp" | |
android:layout_marginEnd="160dp" | |
android:text="+" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/mTxt" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.activity.viewModels | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleCoroutineScope | |
import androidx.lifecycle.lifecycleScope | |
import androidx.lifecycle.repeatOnLifecycle | |
import kotlinx.coroutines.flow.collectLatest | |
import kotlinx.coroutines.launch | |
import me.astrocoder.blog.usingstateflow.databinding.ActivityMainBinding | |
class MainActivity : AppCompatActivity() { | |
lateinit var binding: ActivityMainBinding | |
private val myViewModel: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
//button on click, call our viewModel | |
binding.incrementBtn.setOnClickListener { | |
//increment our counter value: | |
myViewModel.incrementCounter() | |
} | |
//Launch myViewModel in a LifeCycleScope | |
lifecycleScope.launch { | |
//Repeat on LifeCycle ensures that the flow only emits data when needed | |
// (I.e when the app is not in the background, or when the UI is visible), | |
// this is helpful when a user presses the home button and our app goes into the background, | |
// as we don’t want to collect data might put our app at a risk of a crash, | |
// for example showing a modal when the app is in the background can cause it to crash | |
repeatOnLifecycle(Lifecycle.State.STARTED){ | |
myViewModel.counter.collect{ | |
binding.mTxt.text = it.toString() | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import androidx.activity.viewModels | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleCoroutineScope | |
import androidx.lifecycle.lifecycleScope | |
import androidx.lifecycle.repeatOnLifecycle | |
import kotlinx.coroutines.flow.collectLatest | |
import kotlinx.coroutines.launch | |
import me.astrocoder.blog.usingstateflow.databinding.ActivityMainBinding | |
class MainActivity : AppCompatActivity() { | |
lateinit var binding: ActivityMainBinding | |
private val myViewModel: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = ActivityMainBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
//button on click, call our viewModel | |
binding.incrementBtn.setOnClickListener { | |
//increment our counter value: | |
myViewModel.incrementCounter() | |
} | |
//Launch myViewModel in a LifeCycleScope | |
lifecycleScope.launch { | |
//Repeat on LifeCycle ensures that the flow only emits data when needed | |
// (I.e when the app is not in the background, or when the UI is visible), | |
// this is helpful when a user presses the home button and our app goes into the background, | |
// as we don’t want to collect data might put our app at a risk of a crash, | |
// for example showing a modal when the app is in the background can cause it to crash | |
repeatOnLifecycle(Lifecycle.State.STARTED){ | |
myViewModel.counter.collect{ | |
binding.mTxt.text = it.toString() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment