Drawable in ViewModel with MVVM concept












0














I confused with MVVM concept that ViewModel should not reference View.

In my usecase , I have to use Databinding and wrapping the Drawable by LiveData and observe its value in xml view.



Base on suggestion from Android I implemented as below
https://developer.android.com/topic/libraries/architecture/viewmodel




If the ViewModel needs the Application context, for example to find a
system service, it can extend the AndroidViewModel class and have a
constructor that receives the Application in the constructor, since
Application class extends Context.




MyViewModel.kt



class MyViewModel(application: Application): AndroidViewModel(application){  
private val _showIcon = MutableLiveData<Drawable>
val showIcon: LiveData<Drawable>
get() = _showIcon

fun applyChanged(){
if(condition){
_showIcon.value = AppCompatResources.getDrawable(getApplication(),R.drawable.icon1)
}else{
_showIcon.value = null
}
}
}


main_activity.xml



   android:drawableTop="@{viewModel.showIcon}"


Question:

This approach is OK with MVVM concept ? Is there anything I have to do with context inside ViewModel to prevent leak memory problem?

Or any potential problem in my code ?



Thank you so much !










share|improve this question






















  • What if you make BindingAdapter for your drawable loading. In such a way, you can prevent memory leaks for storing context in ViewModel.
    – Jeel Vankhede
    16 hours ago










  • How to make BindingAdapter in my case ?
    – Vegeta
    16 hours ago










  • Please refer here : developer.android.com/topic/libraries/data-binding/…, let me know if problem persist.
    – Jeel Vankhede
    16 hours ago










  • @JeelVankhede I already tried and it worked ! Thank you. But it's not my purpose, I am confused about my approach that has potential problem or it against MVVM concept ???
    – Vegeta
    1 hour ago










  • @JeelVankhede As far as I know, ViewModel should not refer Activity context because viewModel and Activity's lifecycle is difference. But Application's context is OK because Application and ViewModel is same lifecycle. Is that correct ?
    – Vegeta
    1 hour ago
















0














I confused with MVVM concept that ViewModel should not reference View.

In my usecase , I have to use Databinding and wrapping the Drawable by LiveData and observe its value in xml view.



Base on suggestion from Android I implemented as below
https://developer.android.com/topic/libraries/architecture/viewmodel




If the ViewModel needs the Application context, for example to find a
system service, it can extend the AndroidViewModel class and have a
constructor that receives the Application in the constructor, since
Application class extends Context.




MyViewModel.kt



class MyViewModel(application: Application): AndroidViewModel(application){  
private val _showIcon = MutableLiveData<Drawable>
val showIcon: LiveData<Drawable>
get() = _showIcon

fun applyChanged(){
if(condition){
_showIcon.value = AppCompatResources.getDrawable(getApplication(),R.drawable.icon1)
}else{
_showIcon.value = null
}
}
}


main_activity.xml



   android:drawableTop="@{viewModel.showIcon}"


Question:

This approach is OK with MVVM concept ? Is there anything I have to do with context inside ViewModel to prevent leak memory problem?

Or any potential problem in my code ?



Thank you so much !










share|improve this question






















  • What if you make BindingAdapter for your drawable loading. In such a way, you can prevent memory leaks for storing context in ViewModel.
    – Jeel Vankhede
    16 hours ago










  • How to make BindingAdapter in my case ?
    – Vegeta
    16 hours ago










  • Please refer here : developer.android.com/topic/libraries/data-binding/…, let me know if problem persist.
    – Jeel Vankhede
    16 hours ago










  • @JeelVankhede I already tried and it worked ! Thank you. But it's not my purpose, I am confused about my approach that has potential problem or it against MVVM concept ???
    – Vegeta
    1 hour ago










  • @JeelVankhede As far as I know, ViewModel should not refer Activity context because viewModel and Activity's lifecycle is difference. But Application's context is OK because Application and ViewModel is same lifecycle. Is that correct ?
    – Vegeta
    1 hour ago














0












0








0







I confused with MVVM concept that ViewModel should not reference View.

In my usecase , I have to use Databinding and wrapping the Drawable by LiveData and observe its value in xml view.



Base on suggestion from Android I implemented as below
https://developer.android.com/topic/libraries/architecture/viewmodel




If the ViewModel needs the Application context, for example to find a
system service, it can extend the AndroidViewModel class and have a
constructor that receives the Application in the constructor, since
Application class extends Context.




MyViewModel.kt



class MyViewModel(application: Application): AndroidViewModel(application){  
private val _showIcon = MutableLiveData<Drawable>
val showIcon: LiveData<Drawable>
get() = _showIcon

fun applyChanged(){
if(condition){
_showIcon.value = AppCompatResources.getDrawable(getApplication(),R.drawable.icon1)
}else{
_showIcon.value = null
}
}
}


main_activity.xml



   android:drawableTop="@{viewModel.showIcon}"


Question:

This approach is OK with MVVM concept ? Is there anything I have to do with context inside ViewModel to prevent leak memory problem?

Or any potential problem in my code ?



Thank you so much !










share|improve this question













I confused with MVVM concept that ViewModel should not reference View.

In my usecase , I have to use Databinding and wrapping the Drawable by LiveData and observe its value in xml view.



Base on suggestion from Android I implemented as below
https://developer.android.com/topic/libraries/architecture/viewmodel




If the ViewModel needs the Application context, for example to find a
system service, it can extend the AndroidViewModel class and have a
constructor that receives the Application in the constructor, since
Application class extends Context.




MyViewModel.kt



class MyViewModel(application: Application): AndroidViewModel(application){  
private val _showIcon = MutableLiveData<Drawable>
val showIcon: LiveData<Drawable>
get() = _showIcon

fun applyChanged(){
if(condition){
_showIcon.value = AppCompatResources.getDrawable(getApplication(),R.drawable.icon1)
}else{
_showIcon.value = null
}
}
}


main_activity.xml



   android:drawableTop="@{viewModel.showIcon}"


Question:

This approach is OK with MVVM concept ? Is there anything I have to do with context inside ViewModel to prevent leak memory problem?

Or any potential problem in my code ?



Thank you so much !







android mvvm viewmodel android-drawable android-architecture-components






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 17 hours ago









Vegeta

171520




171520












  • What if you make BindingAdapter for your drawable loading. In such a way, you can prevent memory leaks for storing context in ViewModel.
    – Jeel Vankhede
    16 hours ago










  • How to make BindingAdapter in my case ?
    – Vegeta
    16 hours ago










  • Please refer here : developer.android.com/topic/libraries/data-binding/…, let me know if problem persist.
    – Jeel Vankhede
    16 hours ago










  • @JeelVankhede I already tried and it worked ! Thank you. But it's not my purpose, I am confused about my approach that has potential problem or it against MVVM concept ???
    – Vegeta
    1 hour ago










  • @JeelVankhede As far as I know, ViewModel should not refer Activity context because viewModel and Activity's lifecycle is difference. But Application's context is OK because Application and ViewModel is same lifecycle. Is that correct ?
    – Vegeta
    1 hour ago


















  • What if you make BindingAdapter for your drawable loading. In such a way, you can prevent memory leaks for storing context in ViewModel.
    – Jeel Vankhede
    16 hours ago










  • How to make BindingAdapter in my case ?
    – Vegeta
    16 hours ago










  • Please refer here : developer.android.com/topic/libraries/data-binding/…, let me know if problem persist.
    – Jeel Vankhede
    16 hours ago










  • @JeelVankhede I already tried and it worked ! Thank you. But it's not my purpose, I am confused about my approach that has potential problem or it against MVVM concept ???
    – Vegeta
    1 hour ago










  • @JeelVankhede As far as I know, ViewModel should not refer Activity context because viewModel and Activity's lifecycle is difference. But Application's context is OK because Application and ViewModel is same lifecycle. Is that correct ?
    – Vegeta
    1 hour ago
















What if you make BindingAdapter for your drawable loading. In such a way, you can prevent memory leaks for storing context in ViewModel.
– Jeel Vankhede
16 hours ago




What if you make BindingAdapter for your drawable loading. In such a way, you can prevent memory leaks for storing context in ViewModel.
– Jeel Vankhede
16 hours ago












How to make BindingAdapter in my case ?
– Vegeta
16 hours ago




How to make BindingAdapter in my case ?
– Vegeta
16 hours ago












Please refer here : developer.android.com/topic/libraries/data-binding/…, let me know if problem persist.
– Jeel Vankhede
16 hours ago




Please refer here : developer.android.com/topic/libraries/data-binding/…, let me know if problem persist.
– Jeel Vankhede
16 hours ago












@JeelVankhede I already tried and it worked ! Thank you. But it's not my purpose, I am confused about my approach that has potential problem or it against MVVM concept ???
– Vegeta
1 hour ago




@JeelVankhede I already tried and it worked ! Thank you. But it's not my purpose, I am confused about my approach that has potential problem or it against MVVM concept ???
– Vegeta
1 hour ago












@JeelVankhede As far as I know, ViewModel should not refer Activity context because viewModel and Activity's lifecycle is difference. But Application's context is OK because Application and ViewModel is same lifecycle. Is that correct ?
– Vegeta
1 hour ago




@JeelVankhede As far as I know, ViewModel should not refer Activity context because viewModel and Activity's lifecycle is difference. But Application's context is OK because Application and ViewModel is same lifecycle. Is that correct ?
– Vegeta
1 hour ago












1 Answer
1






active

oldest

votes


















1














I don't see any need to use databinding or view models for what you want to do. Just refer the drawable directly in xml file. If it is null, it won't be there. This is valid because you are getting the image resource from your own resources. If you were supposed to get any drawable from server or local database your approach would make sense.






share|improve this answer





















  • Sure ! I know what you mean. However since Android Architecture Component released, we decided to apply MVVM in my project so we have to follow and keep the decision! I also agree with you that it can be done with very simple approach. Anyway, follow ACC Android is better than anything.
    – Vegeta
    15 hours ago










  • From what I know and read from your link, I don't think that approach is what they advise in ACC. I can't find it anywhere... Like Google embassadors said in droidCon 18' London, "You can do it, but that's not a good reason why you should do it".
    – WhiteBuba
    14 hours ago










  • Thank you for the reply ! The reason I want to do that is all of logic should be handled in ViewModel. The Activity view just only see and show the state. Basically, I want ViewModel will control and return exactly Drawable for View then view just show it only . In order to get drawable in ViewModel, viewModel has to refer to Context . So my question is that there any potential problem with this approach.
    – Vegeta
    1 hour ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942990%2fdrawable-in-viewmodel-with-mvvm-concept%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I don't see any need to use databinding or view models for what you want to do. Just refer the drawable directly in xml file. If it is null, it won't be there. This is valid because you are getting the image resource from your own resources. If you were supposed to get any drawable from server or local database your approach would make sense.






share|improve this answer





















  • Sure ! I know what you mean. However since Android Architecture Component released, we decided to apply MVVM in my project so we have to follow and keep the decision! I also agree with you that it can be done with very simple approach. Anyway, follow ACC Android is better than anything.
    – Vegeta
    15 hours ago










  • From what I know and read from your link, I don't think that approach is what they advise in ACC. I can't find it anywhere... Like Google embassadors said in droidCon 18' London, "You can do it, but that's not a good reason why you should do it".
    – WhiteBuba
    14 hours ago










  • Thank you for the reply ! The reason I want to do that is all of logic should be handled in ViewModel. The Activity view just only see and show the state. Basically, I want ViewModel will control and return exactly Drawable for View then view just show it only . In order to get drawable in ViewModel, viewModel has to refer to Context . So my question is that there any potential problem with this approach.
    – Vegeta
    1 hour ago
















1














I don't see any need to use databinding or view models for what you want to do. Just refer the drawable directly in xml file. If it is null, it won't be there. This is valid because you are getting the image resource from your own resources. If you were supposed to get any drawable from server or local database your approach would make sense.






share|improve this answer





















  • Sure ! I know what you mean. However since Android Architecture Component released, we decided to apply MVVM in my project so we have to follow and keep the decision! I also agree with you that it can be done with very simple approach. Anyway, follow ACC Android is better than anything.
    – Vegeta
    15 hours ago










  • From what I know and read from your link, I don't think that approach is what they advise in ACC. I can't find it anywhere... Like Google embassadors said in droidCon 18' London, "You can do it, but that's not a good reason why you should do it".
    – WhiteBuba
    14 hours ago










  • Thank you for the reply ! The reason I want to do that is all of logic should be handled in ViewModel. The Activity view just only see and show the state. Basically, I want ViewModel will control and return exactly Drawable for View then view just show it only . In order to get drawable in ViewModel, viewModel has to refer to Context . So my question is that there any potential problem with this approach.
    – Vegeta
    1 hour ago














1












1








1






I don't see any need to use databinding or view models for what you want to do. Just refer the drawable directly in xml file. If it is null, it won't be there. This is valid because you are getting the image resource from your own resources. If you were supposed to get any drawable from server or local database your approach would make sense.






share|improve this answer












I don't see any need to use databinding or view models for what you want to do. Just refer the drawable directly in xml file. If it is null, it won't be there. This is valid because you are getting the image resource from your own resources. If you were supposed to get any drawable from server or local database your approach would make sense.







share|improve this answer












share|improve this answer



share|improve this answer










answered 15 hours ago









WhiteBuba

263




263












  • Sure ! I know what you mean. However since Android Architecture Component released, we decided to apply MVVM in my project so we have to follow and keep the decision! I also agree with you that it can be done with very simple approach. Anyway, follow ACC Android is better than anything.
    – Vegeta
    15 hours ago










  • From what I know and read from your link, I don't think that approach is what they advise in ACC. I can't find it anywhere... Like Google embassadors said in droidCon 18' London, "You can do it, but that's not a good reason why you should do it".
    – WhiteBuba
    14 hours ago










  • Thank you for the reply ! The reason I want to do that is all of logic should be handled in ViewModel. The Activity view just only see and show the state. Basically, I want ViewModel will control and return exactly Drawable for View then view just show it only . In order to get drawable in ViewModel, viewModel has to refer to Context . So my question is that there any potential problem with this approach.
    – Vegeta
    1 hour ago


















  • Sure ! I know what you mean. However since Android Architecture Component released, we decided to apply MVVM in my project so we have to follow and keep the decision! I also agree with you that it can be done with very simple approach. Anyway, follow ACC Android is better than anything.
    – Vegeta
    15 hours ago










  • From what I know and read from your link, I don't think that approach is what they advise in ACC. I can't find it anywhere... Like Google embassadors said in droidCon 18' London, "You can do it, but that's not a good reason why you should do it".
    – WhiteBuba
    14 hours ago










  • Thank you for the reply ! The reason I want to do that is all of logic should be handled in ViewModel. The Activity view just only see and show the state. Basically, I want ViewModel will control and return exactly Drawable for View then view just show it only . In order to get drawable in ViewModel, viewModel has to refer to Context . So my question is that there any potential problem with this approach.
    – Vegeta
    1 hour ago
















Sure ! I know what you mean. However since Android Architecture Component released, we decided to apply MVVM in my project so we have to follow and keep the decision! I also agree with you that it can be done with very simple approach. Anyway, follow ACC Android is better than anything.
– Vegeta
15 hours ago




Sure ! I know what you mean. However since Android Architecture Component released, we decided to apply MVVM in my project so we have to follow and keep the decision! I also agree with you that it can be done with very simple approach. Anyway, follow ACC Android is better than anything.
– Vegeta
15 hours ago












From what I know and read from your link, I don't think that approach is what they advise in ACC. I can't find it anywhere... Like Google embassadors said in droidCon 18' London, "You can do it, but that's not a good reason why you should do it".
– WhiteBuba
14 hours ago




From what I know and read from your link, I don't think that approach is what they advise in ACC. I can't find it anywhere... Like Google embassadors said in droidCon 18' London, "You can do it, but that's not a good reason why you should do it".
– WhiteBuba
14 hours ago












Thank you for the reply ! The reason I want to do that is all of logic should be handled in ViewModel. The Activity view just only see and show the state. Basically, I want ViewModel will control and return exactly Drawable for View then view just show it only . In order to get drawable in ViewModel, viewModel has to refer to Context . So my question is that there any potential problem with this approach.
– Vegeta
1 hour ago




Thank you for the reply ! The reason I want to do that is all of logic should be handled in ViewModel. The Activity view just only see and show the state. Basically, I want ViewModel will control and return exactly Drawable for View then view just show it only . In order to get drawable in ViewModel, viewModel has to refer to Context . So my question is that there any potential problem with this approach.
– Vegeta
1 hour ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942990%2fdrawable-in-viewmodel-with-mvvm-concept%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas