Bağımlılıkları olan ViewModel'ler oluşturma Android Jetpack'in bir parçası.

ViewModels, bağımlılık eklemenin en iyi uygulamalarını izleyerek bağımlılıkları kurucularına parametre olarak alabilir. Bunlar çoğunlukla alan veya veri katmanlarından gelen türlerdir. ViewModel'leri çerçeve sağladığından bunların örneklerini oluşturmak için özel bir mekanizma gerekir. Bu sistem, ViewModelProvider.Factory arayüzüdür. ViewModel'leri yalnızca bu arayüzün uygulamaları doğru kapsamda örnekleyebilir.

CreationExtras içeren ViewModels

ViewModel sınıfının kurucuunda bağımlılıklar varsa ViewModelProvider.Factory arayüzünü uygulayan bir fabrika sağlayın. ViewModel'in yeni bir örneğini sağlamak için create(Class<T>, CreationExtras) işlevini geçersiz kılın.

CreationExtras, bir ViewModeli kullanmaya başlamanıza yardımcı olacak alakalı bilgilere erişmenizi sağlar. Ekstralardan erişilebilen anahtarların listesi aşağıda verilmiştir:

Anahtar Uygulama işlevi
ViewModelProvider.NewInstanceFactory.VIEW_MODEL_KEY ViewModelProvider.get() öğesine ilettiğiniz özel anahtara erişim sağlar.
ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY Application sınıfı örneğine erişim sağlar.
SavedStateHandleSupport.DEFAULT_ARGS_KEY SavedStateHandle öğesini oluşturmak için kullanmanız gereken bağımsız değişken paketine erişim sağlar.
SavedStateHandleSupport.SAVED_STATE_REGISTRY_OWNER_KEY ViewModel oluşturmak için kullanılan SavedStateRegistryOwner öğesine erişim sağlar.
SavedStateHandleSupport.VIEW_MODEL_STORE_OWNER_KEY ViewModel oluşturmak için kullanılan ViewModelStoreOwner öğesine erişim sağlar.

Yeni bir SavedStateHandle örneği oluşturmak için CreationExtras.createSavedStateHandle().createRecordStateHandle()) işlevini kullanarak bunu ViewModel'e iletin.

Aşağıda, kapsamında Application sınıfı ve bağımlılık olarak SavedStateHandle kapsamındaki bir depository alan bir ViewModel örneğinin nasıl sağlanacağına dair bir örnek verilmiştir:

Kotlin

    import androidx.lifecycle.SavedStateHandle
    import androidx.lifecycle.ViewModel
    import androidx.lifecycle.ViewModelProvider
    import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
    import androidx.lifecycle.createSavedStateHandle
    import androidx.lifecycle.viewmodel.CreationExtras

    class MyViewModel(
        private val myRepository: MyRepository,
        private val savedStateHandle: SavedStateHandle
    ) : ViewModel() {

        // ViewModel logic
        // ...

        // Define ViewModel factory in a companion object
        companion object {

            val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
                @Suppress("UNCHECKED_CAST")
                override fun <T : ViewModel> create(
                    modelClass: Class<T>,
                    extras: CreationExtras
                ): T {
                    // Get the Application object from extras
                    val application = checkNotNull(extras[APPLICATION_KEY])
                    // Create a SavedStateHandle for this ViewModel from extras
                    val savedStateHandle = extras.createSavedStateHandle()

                    return MyViewModel(
                        (application as MyApplication).myRepository,
                        savedStateHandle
                    ) as T
                }
            }
        }
    }

Java

import static androidx.lifecycle.SavedStateHandleSupport.createSavedStateHandle;
import static androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY;

import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.viewmodel.ViewModelInitializer;

public class MyViewModel extends ViewModel {

    public MyViewModel(
        MyRepository myRepository,
        SavedStateHandle savedStateHandle
    ) { /* Init ViewModel here */ }

    static final ViewModelInitializer<MyViewModel> initializer = new ViewModelInitializer<>(
        MyViewModel.class,
        creationExtras -> {
            MyApplication app = (MyApplication) creationExtras.get(APPLICATION_KEY);
            assert app != null;
            SavedStateHandle savedStateHandle = createSavedStateHandle(creationExtras);

            return new MyViewModel(app.getMyRepository(), savedStateHandle);
        }
    );
}

Daha sonra, ViewModel'in bir örneğini alırken bu fabrikayı kullanabilirsiniz:

Kotlin

import androidx.activity.viewModels

class MyActivity : AppCompatActivity() {

    private val viewModel: MyViewModel by viewModels { MyViewModel.Factory }

    // Rest of Activity code
}

Java

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

public class MyActivity extends AppCompatActivity {

MyViewModel myViewModel = new ViewModelProvider(
    this,
    ViewModelProvider.Factory.from(MyViewModel.initializer)
).get(MyViewModel.class);

// Rest of Activity code
} 

Jetpack Compose

import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun MyScreen(
    modifier: Modifier = Modifier,
    viewModel: MyViewModel = viewModel(factory = MyViewModel.Factory)
) {
    // ...
}

Alternatif olarak, daha deyimsel bir Kotlin API'si kullanarak fabrikalar oluşturmak için ViewModel fabrika DSL'sini kullanın:

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.createSavedStateHandle
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory

class MyViewModel(
    private val myRepository: MyRepository,
    private val savedStateHandle: SavedStateHandle
) : ViewModel() {
    // ViewModel logic

    // Define ViewModel factory in a companion object
    companion object {
        val Factory: ViewModelProvider.Factory = viewModelFactory {
            initializer {
                val savedStateHandle = createSavedStateHandle()
                val myRepository = (this[APPLICATION_KEY] as MyApplication).myRepository
                MyViewModel(
                    myRepository = myRepository,
                    savedStateHandle = savedStateHandle
                )
            }
        }
    }
}

2.5.0'dan daha eski ViewModel sürümü için fabrikalar

ViewModel'in 2.5.0'dan eski bir sürümünü kullanıyorsanız ViewModelProvider.Factory'i genişleten bir sınıf alt kümesinden fabrikalar sağlamanız ve create(Class<T>) işlevini uygulamanız gerekir. ViewModel’in ihtiyaç duyduğu bağımlılıklara bağlı olarak, şunlardan farklı bir sınıfın genişletilmesi gerekir:

Application veya SavedStateHandle gerekmiyorsa ViewModelProvider.Factory üzerinden uzatabilirsiniz.

Aşağıdaki örnekte, depoyu alan ViewModel için AbstractSavedStateViewModelFactory ve bağımlılık olarak SavedStateHandle türü kullanılmaktadır:

Kotlin

class MyViewModel(
private val myRepository: MyRepository,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {

// ViewModel logic ...

// Define ViewModel factory in a companion object
companion object {
    fun provideFactory(
        myRepository: MyRepository,
        owner: SavedStateRegistryOwner,
        defaultArgs: Bundle? = null,
    ): AbstractSavedStateViewModelFactory =
        object : AbstractSavedStateViewModelFactory(owner, defaultArgs) {
            @Suppress("UNCHECKED_CAST")
            override fun <T : ViewModel> create(
                key: String,
                modelClass: Class<T>,
                handle: SavedStateHandle
            ): T {
                return MyViewModel(myRepository, handle) as T
            }
        }
    }
}

Java

import androidx.annotation.NonNull;
import androidx.lifecycle.AbstractSavedStateViewModelFactory;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;

public class MyViewModel extends ViewModel {
    public MyViewModel(
        MyRepository myRepository,
        SavedStateHandle savedStateHandle
    ) { /* Init ViewModel here */ }
}

public class MyViewModelFactory extends AbstractSavedStateViewModelFactory {

    private final MyRepository myRepository;

    public MyViewModelFactory(
        MyRepository myRepository
    ) {
        this.myRepository = myRepository;
    }

    @SuppressWarnings("unchecked")
    @NonNull
    @Override
    protected <T extends ViewModel> T create(
        @NonNull String key, @NonNull Class<T> modelClass, @NonNull SavedStateHandle handle
    ) {
        return (T) new MyViewModel(myRepository, handle);
    }
}

Daha sonra, ViewModel'inizi almak için fabrika ayarlarına erişebilirsiniz:

Kotlin

import androidx.activity.viewModels

class MyActivity : AppCompatActivity() {

    private val viewModel: MyViewModel by viewModels {
        MyViewModel.provideFactory((application as MyApplication).myRepository, this)
    }

    // Rest of Activity code
}

Java

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

public class MyActivity extends AppCompatActivity {

    MyViewModel myViewModel = new ViewModelProvider(
        this,
        ViewModelProvider.Factory.from(MyViewModel.initializer)
    ).get(MyViewModel.class);

    // Rest of Activity code
}

Jetpack Compose

import androidx.lifecycle.viewmodel.compose.viewModel

@Composable
fun MyScreen(
    modifier: Modifier = Modifier,
    viewModel: MyViewModel = viewModel(
        factory = MyViewModel.provideFactory(
            (LocalContext.current.applicationContext as MyApplication).myRepository,
            owner = LocalSavedStateRegistryOwner.current
        )
    )
) {
    // ...
}