首页 > 解决方案 > MVVM 注入 Activity 字符串资源阅读器

问题描述

因此,每个人都知道将 Context 引用(不是 Application)传递给ViewModel是一件坏事。就我而言,有些项目需要使用 android 字符串资源表示按字母顺序排序(因此,要阅读它,我需要一个 Activity 上下文)。

推荐的方法是什么?将项目列表从 Activity 传递ViewModel给 Activity 以读取这些字符串并返回到ViewModel看起来确实有点不这样MVVM-ish,并且注入ViewModel字符串资源读取器会泄漏 Context..

对此有什么想法吗?

标签: androidmemory-leaksandroid-resourcesandroid-mvvm

解决方案


一种选择是从AndroidViewModel扩展,它具有对应用程序上下文的引用。然后,您可以使用它Context来加载字符串资源并将它们传送回您的Activity.

public class MyViewModel extends AndroidViewModel {
    private final LiveData<String> stringResource = new MutableLiveData<>();

    public MyViewModel(Application context) {
        super(context);
        statusLabel.setValue(context.getString(R.string.labelString));
    }

    public LiveData<String> getStringResource() {
        return stringResource;
    }
}

但是,正如Jose Alcerreca在这篇 Android Developers Medium Post 中指出的那样,这不是推荐的做法,因为例如,如果Locale更改和 Activity 被重建,ViewModel 将不会对此配置更改做出反应并继续交付过时的字符串(来自上一个Locale)。

因此,建议的方法是仅从 ViewModel 返回资源 id 并获取 Activity 上的字符串。

public class MyViewModel extends ViewModel {
    public final LiveData<Integer> stringResource = new MutableLiveData<>();

    public MyViewModel(Application context) {
        super(context);
        stringResource.setValue(R.string.labelString);
    }

    public LiveData<Integer> getStringResource() {
        return stringResource;
    }
}

更新

由于您必须从您的字符串资源中获取字符串资源,Activity但在您的中应用排序逻辑ViewModel我认为您不能避免将List<String>返回传递给您ViewModel进行排序

public class MyViewModel extends ViewModel {
    public final MutableLiveData<Integer> stringArrayId = new MutableLiveData<>();

    public MyViewModel(Application context) {
        super(context);
        stringArrayId.setValue(R.array.string_array_id);
    }

    public LiveData<Integer> getStringArrayId() {
        return stringArrayId;
    }
}


public class MyActivity extends AppCompatActivity {

    public void onCreate(Bundle savedInstanceState) {
        MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);

        viewModel.getStringArrayId().observe(this, strArrayId -> {
            String[] resolvedStrings = getResources().getStringArray(strArrayId);
            List<String> sortedStrings = viewModel.sortList(Arrays.asList(resolvedStrings));
            updateUi(sortedStrings);
        });
    }
}

如果您认为这还不够 MVVM'ish,也许您可​​以继续解决List<String>您的问题,ViewModel并在排序列表中有一个额外LiveData的内容,每次LiveData保存原始字符串列表更改时都会更新。

public class MyViewModel extends ViewModel {
    public final MutableLiveData<Integer> stringArrayId = new MutableLiveData<>();
    public final MutableLiveData<List<String>> stringsList = new MutableLiveData<>();
    public final LiveData<List<String>> sortedStringList;


    public MyViewModel(Application context) {
        super(context);
        stringArrayId.setValue(R.array.string_array_id);

        sortedStringList = Transformations.map(stringsList, l -> {
            Collections.sort(l);
            return l; 
        });
    }

    public LiveData<Integer> getStringArrayId() {
        return stringArrayId;
    }

    public LiveData<List<String>> sortedStringList() {
        return sortedStringList;
    }

    public void setStringsList(List<String> resolvedStrings) {
        stringsList.setValue(resolvedStrings);
    }
}


public class MyActivity extends AppCompatActivity {

    public void onCreate(Bundle savedInstanceState) {
        MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);

        viewModel.getStringArrayId().observe(this, strArrayId -> {
            String[] resolvedStrings = getResources().getStringArray(strArrayId);
            viewModel.setStringsList(Arrays.asList(resolvedStrings));
        });

        viewModel.sortedStringList().observe(this, sortedStrings -> updateUi(sortedStrings));
    }
}

对我来说感觉设计过度了,你仍然需要将它发送List<String>回你的ViewModel. 但是,如果排序顺序取决于可以在运行时更改的过滤器,那么采用这种方式可能会有所帮助。然后,您可以添加一个MediatorLiveData以在过滤器更改或字符串列表更改时做出反应,然后您的视图只需将这些更改通知给ViewModel并且将观察排序列表。


推荐阅读