Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – How can I make it live data observer working based on user click event with take some user input, but should start observing from onCreate?

Posted on November 11, 2022 By

Solution 1 :

At the time when you are calling

mutableLiveData = manualLicenseRepository.getLicenseData(licenseKey, macAddress, productId);

you have already set active observer in onCreate to the instance of mutableLiveData. After you set new instance of mutableLiveData all the observers will be unsubscribed, since the previous instance does not exist anymore, so your observer will not receive any results.

Better way of sending data to observers would be MutableLiveData.postValue(value). In this case instead of creating new live data instance and unsubscribe observers you are just posting a value to the active observers.

But if you need to get license data only once after user press button, you should better call Room suspend function and get plain data to display

P.S. it’s better to add observers in onViewCreated not in onCreate

Problem :

I have retrofit then repository then view model and then view, but while clicking on button observer don’t observe in onChanged. OnClicking on button from onClick, everything is working. I’m getting API response in logcat, but in onChanged it is not called!

ManualLicenseKeyViewModel

Code:

public class ManualLicenseKeyViewModel extends ViewModel {
    public MutableLiveData<String> key = new MutableLiveData<>();
    private MutableLiveData<License> mutableLiveData;
    private ManualLicenseRepository manualLicenseRepository;


    public void init() {
        if (mutableLiveData == null) {
            manualLicenseRepository = ManualLicenseRepository.getInstance();
        }
    }

    public LiveData<License> getLicenseRepository() {
        return mutableLiveData;
    }

    private void getLicenseData(String licenseKey, String macAddress, int productId) {
        mutableLiveData = manualLicenseRepository.getLicenseData(licenseKey, macAddress, productId);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                try {
                    getLicenseData(key.getValue(), "null", FIXED_PRODUCT_ID);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
                break;


        }
    }

}

activity – onCreate:

protected void init() {
        manualLicenseKeyViewModel.init();
        manualLicenseKeyViewModel.getLicenseRepository().observe(this, this);
    }


  @Override
    public void onChanged(License license) {
        showLog("Working?");
        try {
            showLog("license: " + license.getLicensekey());
        } catch (Exception e) {

        }
    }

NOTE: It is working in below case if I pass in init method, but the problem is, I want to perform in user click event and take value from edit text before submit button. Also I don’t want to observe in view model itself, because I want data on activity!

 public void init() {
        if (mutableLiveData == null) {
            manualLicenseRepository = ManualLicenseRepository.getInstance();
            mutableLiveData = manualLicenseRepository.getLicenseData("QQQQQ", "null", 4);
        }
    }

Explanation once again:

READ  [FIXED] c# - Attempting to debug Xamarin Forms app results in instantly closing app on physical device and "failed to connect to socket error"
Powered by Inline Related Posts

The problem is if I write observe statement in onCreate, it won’t observe based on user click event. Because I want to only call repository API when user fill value in edit text and submit button, then only I can get value which I have to pass in repository to make API call and pass that value to API.

Android Tags:android, android-livedata, java

Post navigation

Previous Post: [FIXED] activity_main.xml for android studio stuck on loading
Next Post: Can a beginner learn Moonlight Sonata 1st Movement?

Related Posts

[FIXED] gradle – React native android Task :app:transformClassesWithMultidexlistForRelease FAILED Android
[FIXED] java – Android animate layouts and views Android
[FIXED] java – myGridLayout = findViewById(R.id.myGridLayout); crashes my app Android
[FIXED] java – notifyItemInserted() is having same effect on list as notifyDataSetChanged() when I only want last item in list updated Android
[FIXED] kotlin – How to transfer plain text via Android NFC? Android
[FIXED] c# – Why can I not initialise a VideoView object by the FindViewById method? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme