This file contains hidden or 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
class MyViewModel() : ViewModel() { | |
fun initialize() { | |
viewModelScope.launch { | |
processBitmap() | |
} | |
} | |
suspend fun processBitmap() = withContext(Dispatchers.Default) { | |
// Do your long running work here |
This file contains hidden or 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
// Don't use GlobalScope - for example purposes only | |
GlobalScope.launch { | |
longRunningFunction() | |
anotherLongRunningFunction() | |
} |
This file contains hidden or 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
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<data> | |
<variable name="viewmodel" | |
type="com.android.MyViewModel"/> | |
</data> | |
<TextView | |
android:id="@+id/name" | |
android:text="@{viewmodel.name}" | |
android:layout_height="wrap_content" | |
android:layout_width="wrap_content"/> |
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
// This ktx requires at least androidx.activity:activity-ktx:1.0.0 | |
private val myViewModel: MyViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
//Inflate view and create binding | |
val binding: MainActivityBinding = |
This file contains hidden or 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
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<data> | |
<variable name="viewmodel" | |
type="com.android.MyViewModel"/> | |
</data> | |
<... Rest of your layout ...> | |
</layout> | |
This file contains hidden or 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
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
myViewModel.name.observe(this, { newName -> | |
// Update the UI. In this case, a TextView. | |
nameTextView.text = newName | |
}) | |
} |
This file contains hidden or 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
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// Other fragment setup code | |
NavController navController = NavHostFragment.findNavController(this); | |
ViewModelProvider viewModelProvider = new ViewModelProvider(this, | |
navController.getViewModelStore(R.id.checkout_graph)); | |
This file contains hidden or 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
val viewModel: CheckoutViewModel by navGraphViewModels(R.id.checkout_graph) |
This file contains hidden or 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
// Any fragment's onCreate or onActivityCreated | |
// This ktx requires at least androidx.fragment:fragment-ktx:1.1.0 | |
val sharedViewModel: ActivityViewModel by activityViewModels() |
NewerOlder