API Cakupan ViewModel (View)   Bagian dari Android Jetpack.

Konsep dan penerapan Jetpack Compose

Cakupan adalah kunci untuk menggunakan ViewModel secara efektif. Setiap ViewModel dicakup ke objek yang menerapkan antarmuka ViewModelStoreOwner. Ada beberapa API yang memungkinkan Anda mengelola cakupan ViewModel dengan lebih mudah.

Metode ViewModelProvider.get() memungkinkan Anda mendapatkan instance ViewModel yang dicakupkan ke ViewModelStoreOwner apa pun. Untuk pengguna Kotlin, terdapat berbagai fungsi ekstensi yang tersedia untuk kasus penggunaan paling umum. Semua penerapan fungsi ekstensi Kotlin menggunakan ViewModelProvider API di balik layar.

ViewModel yang dicakupkan ke ViewModelStoreOwner terdekat

Anda dapat menentukan cakupan ViewModel ke Activity, Fragment, atau tujuan grafik Navigation. Fungsi ekstensi yang disediakan oleh library Activity, Fragment, dan Navigation memungkinkan Anda mendapatkan instance ViewModel yang dicakupkan ke ViewModelStoreOwner.viewModels()

View

import androidx.activity.viewModels

class MyActivity : AppCompatActivity() {
    // ViewModel API available in activity.activity-ktx
    // The ViewModel is scoped to `this` Activity
    val viewModel: MyViewModel by viewModels()
}

import androidx.fragment.app.viewModels

class MyFragment : Fragment() {
    // ViewModel API available in fragment.fragment-ktx
    // The ViewModel is scoped to `this` Fragment
    val viewModel: MyViewModel by viewModels()
}

View

import androidx.lifecycle.ViewModelProvider;

public class MyActivity extends AppCompatActivity {
    // The ViewModel is scoped to `this` Activity
    MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
}

public class MyFragment extends Fragment {
    // The ViewModel is scoped to `this` Fragment
    MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
}

ViewModel yang dicakupkan ke ViewModelStoreOwner apa pun

Fungsi ComponentActivity.viewModels() dan Fragment.viewModels() dalam sistem View menggunakan parameter ownerProducer opsional yang dapat Anda gunakan untuk menentukan ViewModelStoreOwner yang dicakupkan ke instance ViewModel. Contoh berikut menunjukkan cara mendapatkan instance ViewModel yang dicakupkan ke fragmen induk:

View

import androidx.fragment.app.viewModels

class MyFragment : Fragment() {

    // ViewModel API available in fragment.fragment-ktx
    // The ViewModel is scoped to the parent of `this` Fragment
    val viewModel: SharedViewModel by viewModels(
        ownerProducer = { requireParentFragment() }
    )
}

View

import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // The ViewModel is scoped to the parent of `this` Fragment
        viewModel = new ViewModelProvider(requireParentFragment())
            .get(SharedViewModel.class);
    }
}

Mendapatkan ViewModel Cakupan Activity dari Fragment adalah kasus penggunaan umum. Saat melakukannya, Anda dapat menggunakan fungsi ekstensi activityViewModels() View. Jika tidak menggunakan View dan Kotlin, Anda dapat menggunakan API yang sama seperti di atas dan dengan meneruskan pemilik yang tepat.

View

import androidx.fragment.app.activityViewModels

class MyFragment : Fragment() {

    // ViewModel API available in fragment.fragment-ktx
    // The ViewModel is scoped to the host Activity
    val viewModel: SharedViewModel by activityViewModels()
}

View

import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // The ViewModel is scoped to the host Activity
        viewModel = new ViewModelProvider(requireActivity())
            .get(SharedViewModel.class);
    }
}

ViewModel yang dicakupkan ke grafik Navigation

Grafik Navigation juga merupakan pemilik penyimpanan ViewModel. Jika menggunakan Navigation Fragment, Anda bisa mendapatkan instance ViewModel yang dicakupkan ke grafik Navigation dengan fungsi ekstensi navGraphViewModels(graphId) View.

View

import androidx.navigation.navGraphViewModels

class MyFragment : Fragment() {

    // ViewModel API available in navigation.navigation-fragment
    // The ViewModel is scoped to the `nav_graph` Navigation graph
    val viewModel: SharedViewModel by navGraphViewModels(R.id.nav_graph)

    // Equivalent navGraphViewModels code using the viewModels API
    val viewModel: SharedViewModel by viewModels(
        { findNavController().getBackStackEntry(R.id.nav_graph) }
    )
}

View

import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        NavController navController = NavHostFragment.findNavController(this);
        NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.my_graph);

        // The ViewModel is scoped to the `nav_graph` Navigation graph
        viewModel = new ViewModelProvider(backStackEntry).get(SharedViewModel.class);
    }
}

Jika menggunakan Hilt sebagai tambahan Jetpack Navigation, Anda dapat menggunakan hiltNavGraphViewModels(graphId) API sebagai berikut.

View

import androidx.hilt.navigation.fragment.hiltNavGraphViewModels

class MyFragment : Fragment() {

    // ViewModel API available in hilt.hilt-navigation-fragment
    // The ViewModel is scoped to the `nav_graph` Navigation graph
    // and is provided using the Hilt-generated ViewModel factory
    val viewModel: SharedViewModel by hiltNavGraphViewModels(R.id.nav_graph)
}

View

import androidx.hilt.navigation.HiltViewModelFactory;
import androidx.lifecycle.ViewModelProvider;

public class MyFragment extends Fragment {

    SharedViewModel viewModel;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        NavController navController = NavHostFragment.findNavController(this);
        NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.my_graph);

        // The ViewModel is scoped to the `nav_graph` Navigation graph
        // and is provided using the Hilt-generated ViewModel factory
        viewModel = new ViewModelProvider(
            backStackEntry,
            HiltViewModelFactory.create(getContext(), backStackEntry)
        ).get(SharedViewModel.class);
    }
}