Norway


Hi,

We came from Loader/ Retained instance fragment era background 🙂 11; https://www.reddit.com/r/androiddev/comments/86a296/do_you_mind_to_share_your_experience_and_user/

Recently, we want to kick start a new project, using / as replacement to our previous Loader/ Retained instance fragment mythology.

We notice when constructing ViewModel, which using non default arg constructor, most tutorial recommend using 2 ( injection)

We have a feeling, using DI will over our project, and make the code less readable.

We prefer to go for a more straight forward approach.

class MyViewModel extends ViewModel {
    public MyViewModel(Integer startingScore) {        
    }
}

class MyVMFactory implements ViewModelProvider.Factory {
    private final Integer startingScore;

    public MyVMFactory(Integer startingScore) {
        this.startingScore = startingScore;
    }

    @Override
    public MyViewModel create(Class modelClass) {
        return new MyViewModel(startingScore);
    }
}

class MainActivity {
    void onCreate(Bundle savedInstanceState) {
        MyVMFactory factory = new MyVMFactory(startingScore);
        mViewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
    }
}

I was wondering, do you use any DI, when LiveData/ ViewModel in production project? Do you it over-complicated your project, or it does provide more good than harm, compared with new method?



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here