Update viewmodel from a different classWhat exactly is the difference between Android View Model and a Singleton classStop EditText from gaining focus at Activity startupWhat is the difference between “px”, “dip”, “dp” and “sp”?What is the difference between match_parent and fill_parent?Getting an exception when app startsGoogle map API v2Can someone show me a simple working implementation of PagerSlidingTabStrip?How to read Heart rate from Android WearAdding Social Media Share Logic From Firebase in AndroidThis use Navigation Drawer, and use Tab, and use Fragment. how communication between FragmentActivity and Fragment?How to add child(Product) under a child(Store) in Firebase Database using RecyclerView
How to import .txt file with missing data?
In Pandemic, why take the extra step of eradicating a disease after you've cured it?
What class is best to play when a level behind the rest of the party?
What do I need to do, tax-wise, for a sudden windfall?
When editor does not respond to the request for withdrawal
Dedicated bike GPS computer over smartphone
In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?
Boss making me feel guilty for leaving the company at the end of my internship
Is fission/fusion to iron the most efficient way to convert mass to energy?
Placement of positioning lights on A320 winglets
Am I allowed to determine tenets of my contract as a warlock?
What does BREAD stand for while drafting?
How can religions without a hell discourage evil-doing?
Keeping track of theme when improvising
How do I properly use a function under a class?
What is Gilligan's full name?
What is the theme of analysis?
Is plausible to have subspecies with & without separate sexes?
Is it good practice to create tables dynamically?
Is it a good security practice to force employees hide their employer to avoid being targeted?
Can you open the door or die? v2
Must I use my personal social media account for work?
How can I find out about the game world without meta-influencing it?
Changing the PK column of a data extension without completely recreating it
Update viewmodel from a different class
What exactly is the difference between Android View Model and a Singleton classStop EditText from gaining focus at Activity startupWhat is the difference between “px”, “dip”, “dp” and “sp”?What is the difference between match_parent and fill_parent?Getting an exception when app startsGoogle map API v2Can someone show me a simple working implementation of PagerSlidingTabStrip?How to read Heart rate from Android WearAdding Social Media Share Logic From Firebase in AndroidThis use Navigation Drawer, and use Tab, and use Fragment. how communication between FragmentActivity and Fragment?How to add child(Product) under a child(Store) in Firebase Database using RecyclerView
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am trying to understand ViewModel and LiveData.
In MainActivity, I am observing LiveData
In MyTask, I am setting data on the LiveData, that should be displayed in the activity.
Problem is data set in MyTask is not getting updated on the UI.
MainActivity
public class MainActivity extends AppCompatActivity
private MyViewModel viewModel;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2 = findViewById(R.id.textView2);
viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
viewModel.setNameData("Button clicked");
new MyTask(getApplication()).execute();
);
viewModel.getNameData().observe(this, new Observer<String>()
@Override
public void onChanged(@Nullable String s)
tv2.setText(s);
);
ViewModel class
public class MyViewModel extends AndroidViewModel
private MutableLiveData<String> nameData = new MutableLiveData<>();
public MutableLiveData<String> getNameData()
return nameData;
public void setNameData(String name)
nameData.postValue(name);
public MyViewModel(@NonNull Application application)
super(application);
MyTask class
public class MyTask extends AsyncTask<Void, Void, Void>
private MyViewModel viewModel;
public MyTask(Application application)
viewModel = new MyViewModel(application);
@Override
protected Void doInBackground(Void... voids)
try
Thread.sleep(2000);
catch (InterruptedException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void aVoid)
super.onPostExecute(aVoid);
viewModel.setNameData("Done task");
android android-livedata android-viewmodel
|
show 2 more comments
I am trying to understand ViewModel and LiveData.
In MainActivity, I am observing LiveData
In MyTask, I am setting data on the LiveData, that should be displayed in the activity.
Problem is data set in MyTask is not getting updated on the UI.
MainActivity
public class MainActivity extends AppCompatActivity
private MyViewModel viewModel;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2 = findViewById(R.id.textView2);
viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
viewModel.setNameData("Button clicked");
new MyTask(getApplication()).execute();
);
viewModel.getNameData().observe(this, new Observer<String>()
@Override
public void onChanged(@Nullable String s)
tv2.setText(s);
);
ViewModel class
public class MyViewModel extends AndroidViewModel
private MutableLiveData<String> nameData = new MutableLiveData<>();
public MutableLiveData<String> getNameData()
return nameData;
public void setNameData(String name)
nameData.postValue(name);
public MyViewModel(@NonNull Application application)
super(application);
MyTask class
public class MyTask extends AsyncTask<Void, Void, Void>
private MyViewModel viewModel;
public MyTask(Application application)
viewModel = new MyViewModel(application);
@Override
protected Void doInBackground(Void... voids)
try
Thread.sleep(2000);
catch (InterruptedException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void aVoid)
super.onPostExecute(aVoid);
viewModel.setNameData("Done task");
android android-livedata android-viewmodel
Don't create a new instance ofMyViewModel
in your task, pass in the one you obtained in your Activity
– EpicPandaForce
Mar 25 at 0:35
@EpicPandaForce Is there a way I can create an instance in task without passing? I know it's not a good idea to have singleton ViewModel class
– dcanh121
Mar 25 at 0:38
Just donew MyTask(viewModel)
– EpicPandaForce
Mar 25 at 0:42
Yes, I am trying to explore other ways, in which I do not want to pass any activity reference.
– dcanh121
Mar 25 at 0:44
You're not passing the Activity reference, you're passing the ViewModel reference. 🤔
– EpicPandaForce
Mar 25 at 1:01
|
show 2 more comments
I am trying to understand ViewModel and LiveData.
In MainActivity, I am observing LiveData
In MyTask, I am setting data on the LiveData, that should be displayed in the activity.
Problem is data set in MyTask is not getting updated on the UI.
MainActivity
public class MainActivity extends AppCompatActivity
private MyViewModel viewModel;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2 = findViewById(R.id.textView2);
viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
viewModel.setNameData("Button clicked");
new MyTask(getApplication()).execute();
);
viewModel.getNameData().observe(this, new Observer<String>()
@Override
public void onChanged(@Nullable String s)
tv2.setText(s);
);
ViewModel class
public class MyViewModel extends AndroidViewModel
private MutableLiveData<String> nameData = new MutableLiveData<>();
public MutableLiveData<String> getNameData()
return nameData;
public void setNameData(String name)
nameData.postValue(name);
public MyViewModel(@NonNull Application application)
super(application);
MyTask class
public class MyTask extends AsyncTask<Void, Void, Void>
private MyViewModel viewModel;
public MyTask(Application application)
viewModel = new MyViewModel(application);
@Override
protected Void doInBackground(Void... voids)
try
Thread.sleep(2000);
catch (InterruptedException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void aVoid)
super.onPostExecute(aVoid);
viewModel.setNameData("Done task");
android android-livedata android-viewmodel
I am trying to understand ViewModel and LiveData.
In MainActivity, I am observing LiveData
In MyTask, I am setting data on the LiveData, that should be displayed in the activity.
Problem is data set in MyTask is not getting updated on the UI.
MainActivity
public class MainActivity extends AppCompatActivity
private MyViewModel viewModel;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv2 = findViewById(R.id.textView2);
viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
viewModel.setNameData("Button clicked");
new MyTask(getApplication()).execute();
);
viewModel.getNameData().observe(this, new Observer<String>()
@Override
public void onChanged(@Nullable String s)
tv2.setText(s);
);
ViewModel class
public class MyViewModel extends AndroidViewModel
private MutableLiveData<String> nameData = new MutableLiveData<>();
public MutableLiveData<String> getNameData()
return nameData;
public void setNameData(String name)
nameData.postValue(name);
public MyViewModel(@NonNull Application application)
super(application);
MyTask class
public class MyTask extends AsyncTask<Void, Void, Void>
private MyViewModel viewModel;
public MyTask(Application application)
viewModel = new MyViewModel(application);
@Override
protected Void doInBackground(Void... voids)
try
Thread.sleep(2000);
catch (InterruptedException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void aVoid)
super.onPostExecute(aVoid);
viewModel.setNameData("Done task");
android android-livedata android-viewmodel
android android-livedata android-viewmodel
asked Mar 25 at 0:10
dcanh121dcanh121
3,00273075
3,00273075
Don't create a new instance ofMyViewModel
in your task, pass in the one you obtained in your Activity
– EpicPandaForce
Mar 25 at 0:35
@EpicPandaForce Is there a way I can create an instance in task without passing? I know it's not a good idea to have singleton ViewModel class
– dcanh121
Mar 25 at 0:38
Just donew MyTask(viewModel)
– EpicPandaForce
Mar 25 at 0:42
Yes, I am trying to explore other ways, in which I do not want to pass any activity reference.
– dcanh121
Mar 25 at 0:44
You're not passing the Activity reference, you're passing the ViewModel reference. 🤔
– EpicPandaForce
Mar 25 at 1:01
|
show 2 more comments
Don't create a new instance ofMyViewModel
in your task, pass in the one you obtained in your Activity
– EpicPandaForce
Mar 25 at 0:35
@EpicPandaForce Is there a way I can create an instance in task without passing? I know it's not a good idea to have singleton ViewModel class
– dcanh121
Mar 25 at 0:38
Just donew MyTask(viewModel)
– EpicPandaForce
Mar 25 at 0:42
Yes, I am trying to explore other ways, in which I do not want to pass any activity reference.
– dcanh121
Mar 25 at 0:44
You're not passing the Activity reference, you're passing the ViewModel reference. 🤔
– EpicPandaForce
Mar 25 at 1:01
Don't create a new instance of
MyViewModel
in your task, pass in the one you obtained in your Activity– EpicPandaForce
Mar 25 at 0:35
Don't create a new instance of
MyViewModel
in your task, pass in the one you obtained in your Activity– EpicPandaForce
Mar 25 at 0:35
@EpicPandaForce Is there a way I can create an instance in task without passing? I know it's not a good idea to have singleton ViewModel class
– dcanh121
Mar 25 at 0:38
@EpicPandaForce Is there a way I can create an instance in task without passing? I know it's not a good idea to have singleton ViewModel class
– dcanh121
Mar 25 at 0:38
Just do
new MyTask(viewModel)
– EpicPandaForce
Mar 25 at 0:42
Just do
new MyTask(viewModel)
– EpicPandaForce
Mar 25 at 0:42
Yes, I am trying to explore other ways, in which I do not want to pass any activity reference.
– dcanh121
Mar 25 at 0:44
Yes, I am trying to explore other ways, in which I do not want to pass any activity reference.
– dcanh121
Mar 25 at 0:44
You're not passing the Activity reference, you're passing the ViewModel reference. 🤔
– EpicPandaForce
Mar 25 at 1:01
You're not passing the Activity reference, you're passing the ViewModel reference. 🤔
– EpicPandaForce
Mar 25 at 1:01
|
show 2 more comments
2 Answers
2
active
oldest
votes
Instead of creating a new instance just pass the created instance of viewmodel
public MyTask(MyViewModel myViewModel)
viewmodel = myViewModel;
And then try to update the Ui
To update the observer the viewmodel needs the activity context ..inside which the livedata is observed.. you are creating a separate instance of the viewmodel inside the AsyncTask..
It's already there in the MainActivity. I am creating using viewModel = ViewModelProviders.of(this).get(MyViewModel.class); In MyTask, I am using new keyword.
– dcanh121
Mar 25 at 0:16
You are using application context ...that's not the right way ... In the contractor you should pass activity context...
– Santanu Sur
Mar 25 at 0:18
@dcanh21 please check the update
– Santanu Sur
Mar 25 at 0:21
I want to use AndroidViewModel which uses the application. To set the data in LiveModel, why will I need activity?
– dcanh121
Mar 25 at 0:22
Please check the update
– Santanu Sur
Mar 25 at 0:27
|
show 5 more comments
According to https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
"Instead of trying to solve this with libraries or extensions to the
Architecture Components, it should be faced as a design problem. We
recommend you treat your events as part of your state."
We should not share the livedata or viewmodel instance.
Activity -> Viewmodel -> Repository
Activity should contain instance of a viewModel. If the button is clicked, it should be notified to the viewModel which shall start the task. After getting response in viewmodel, update the livedata. It will automatically get notified in the Activity.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329800%2fupdate-viewmodel-from-a-different-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of creating a new instance just pass the created instance of viewmodel
public MyTask(MyViewModel myViewModel)
viewmodel = myViewModel;
And then try to update the Ui
To update the observer the viewmodel needs the activity context ..inside which the livedata is observed.. you are creating a separate instance of the viewmodel inside the AsyncTask..
It's already there in the MainActivity. I am creating using viewModel = ViewModelProviders.of(this).get(MyViewModel.class); In MyTask, I am using new keyword.
– dcanh121
Mar 25 at 0:16
You are using application context ...that's not the right way ... In the contractor you should pass activity context...
– Santanu Sur
Mar 25 at 0:18
@dcanh21 please check the update
– Santanu Sur
Mar 25 at 0:21
I want to use AndroidViewModel which uses the application. To set the data in LiveModel, why will I need activity?
– dcanh121
Mar 25 at 0:22
Please check the update
– Santanu Sur
Mar 25 at 0:27
|
show 5 more comments
Instead of creating a new instance just pass the created instance of viewmodel
public MyTask(MyViewModel myViewModel)
viewmodel = myViewModel;
And then try to update the Ui
To update the observer the viewmodel needs the activity context ..inside which the livedata is observed.. you are creating a separate instance of the viewmodel inside the AsyncTask..
It's already there in the MainActivity. I am creating using viewModel = ViewModelProviders.of(this).get(MyViewModel.class); In MyTask, I am using new keyword.
– dcanh121
Mar 25 at 0:16
You are using application context ...that's not the right way ... In the contractor you should pass activity context...
– Santanu Sur
Mar 25 at 0:18
@dcanh21 please check the update
– Santanu Sur
Mar 25 at 0:21
I want to use AndroidViewModel which uses the application. To set the data in LiveModel, why will I need activity?
– dcanh121
Mar 25 at 0:22
Please check the update
– Santanu Sur
Mar 25 at 0:27
|
show 5 more comments
Instead of creating a new instance just pass the created instance of viewmodel
public MyTask(MyViewModel myViewModel)
viewmodel = myViewModel;
And then try to update the Ui
To update the observer the viewmodel needs the activity context ..inside which the livedata is observed.. you are creating a separate instance of the viewmodel inside the AsyncTask..
Instead of creating a new instance just pass the created instance of viewmodel
public MyTask(MyViewModel myViewModel)
viewmodel = myViewModel;
And then try to update the Ui
To update the observer the viewmodel needs the activity context ..inside which the livedata is observed.. you are creating a separate instance of the viewmodel inside the AsyncTask..
edited Mar 25 at 0:42
answered Mar 25 at 0:15
Santanu SurSantanu Sur
4,2573831
4,2573831
It's already there in the MainActivity. I am creating using viewModel = ViewModelProviders.of(this).get(MyViewModel.class); In MyTask, I am using new keyword.
– dcanh121
Mar 25 at 0:16
You are using application context ...that's not the right way ... In the contractor you should pass activity context...
– Santanu Sur
Mar 25 at 0:18
@dcanh21 please check the update
– Santanu Sur
Mar 25 at 0:21
I want to use AndroidViewModel which uses the application. To set the data in LiveModel, why will I need activity?
– dcanh121
Mar 25 at 0:22
Please check the update
– Santanu Sur
Mar 25 at 0:27
|
show 5 more comments
It's already there in the MainActivity. I am creating using viewModel = ViewModelProviders.of(this).get(MyViewModel.class); In MyTask, I am using new keyword.
– dcanh121
Mar 25 at 0:16
You are using application context ...that's not the right way ... In the contractor you should pass activity context...
– Santanu Sur
Mar 25 at 0:18
@dcanh21 please check the update
– Santanu Sur
Mar 25 at 0:21
I want to use AndroidViewModel which uses the application. To set the data in LiveModel, why will I need activity?
– dcanh121
Mar 25 at 0:22
Please check the update
– Santanu Sur
Mar 25 at 0:27
It's already there in the MainActivity. I am creating using viewModel = ViewModelProviders.of(this).get(MyViewModel.class); In MyTask, I am using new keyword.
– dcanh121
Mar 25 at 0:16
It's already there in the MainActivity. I am creating using viewModel = ViewModelProviders.of(this).get(MyViewModel.class); In MyTask, I am using new keyword.
– dcanh121
Mar 25 at 0:16
You are using application context ...that's not the right way ... In the contractor you should pass activity context...
– Santanu Sur
Mar 25 at 0:18
You are using application context ...that's not the right way ... In the contractor you should pass activity context...
– Santanu Sur
Mar 25 at 0:18
@dcanh21 please check the update
– Santanu Sur
Mar 25 at 0:21
@dcanh21 please check the update
– Santanu Sur
Mar 25 at 0:21
I want to use AndroidViewModel which uses the application. To set the data in LiveModel, why will I need activity?
– dcanh121
Mar 25 at 0:22
I want to use AndroidViewModel which uses the application. To set the data in LiveModel, why will I need activity?
– dcanh121
Mar 25 at 0:22
Please check the update
– Santanu Sur
Mar 25 at 0:27
Please check the update
– Santanu Sur
Mar 25 at 0:27
|
show 5 more comments
According to https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
"Instead of trying to solve this with libraries or extensions to the
Architecture Components, it should be faced as a design problem. We
recommend you treat your events as part of your state."
We should not share the livedata or viewmodel instance.
Activity -> Viewmodel -> Repository
Activity should contain instance of a viewModel. If the button is clicked, it should be notified to the viewModel which shall start the task. After getting response in viewmodel, update the livedata. It will automatically get notified in the Activity.
add a comment |
According to https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
"Instead of trying to solve this with libraries or extensions to the
Architecture Components, it should be faced as a design problem. We
recommend you treat your events as part of your state."
We should not share the livedata or viewmodel instance.
Activity -> Viewmodel -> Repository
Activity should contain instance of a viewModel. If the button is clicked, it should be notified to the viewModel which shall start the task. After getting response in viewmodel, update the livedata. It will automatically get notified in the Activity.
add a comment |
According to https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
"Instead of trying to solve this with libraries or extensions to the
Architecture Components, it should be faced as a design problem. We
recommend you treat your events as part of your state."
We should not share the livedata or viewmodel instance.
Activity -> Viewmodel -> Repository
Activity should contain instance of a viewModel. If the button is clicked, it should be notified to the viewModel which shall start the task. After getting response in viewmodel, update the livedata. It will automatically get notified in the Activity.
According to https://medium.com/androiddevelopers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54
"Instead of trying to solve this with libraries or extensions to the
Architecture Components, it should be faced as a design problem. We
recommend you treat your events as part of your state."
We should not share the livedata or viewmodel instance.
Activity -> Viewmodel -> Repository
Activity should contain instance of a viewModel. If the button is clicked, it should be notified to the viewModel which shall start the task. After getting response in viewmodel, update the livedata. It will automatically get notified in the Activity.
answered Apr 1 at 18:12
dcanh121dcanh121
3,00273075
3,00273075
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329800%2fupdate-viewmodel-from-a-different-class%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Don't create a new instance of
MyViewModel
in your task, pass in the one you obtained in your Activity– EpicPandaForce
Mar 25 at 0:35
@EpicPandaForce Is there a way I can create an instance in task without passing? I know it's not a good idea to have singleton ViewModel class
– dcanh121
Mar 25 at 0:38
Just do
new MyTask(viewModel)
– EpicPandaForce
Mar 25 at 0:42
Yes, I am trying to explore other ways, in which I do not want to pass any activity reference.
– dcanh121
Mar 25 at 0:44
You're not passing the Activity reference, you're passing the ViewModel reference. 🤔
– EpicPandaForce
Mar 25 at 1:01