Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Not call twice: result of call Single.just() from ViewModel to Activity

Posted on November 14, 2022

Solution 1 :

First of all Single.just() operator is a static method which creates a new object of SingleJust<T> class which you can subscribe to it, calling it twice doesn’t mean that it will emit two items, it just creates a new object and assign it to isShowProgressSingle.

What you looking for is Observable or Flowable of items which can emit more than one item, for example :

 isShowProgressObservable = Observable.fromArray(true,false)

And if you want to create a stream for showing your progress a simple solution is ReplaySubject which is a subclass of Observable, you can create and use it like this :

 val progress = ReplaySubject.create<Boolean>()
 progress.onNext(true)
 progress.onNext(false)
 progress.subscribe{
      //do your thing
 }

Problem :

Android Studio
In build.gradle:

   implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'

In my ViewModel I test this:

import android.app.Application
import android.view.View
import androidx.lifecycle.AndroidViewModel
import io.reactivex.Single

    class FilmsRxJavaViewModel(application: Application) : AndroidViewModel(application) {
       
        init {
            Debug.d(TAG, "init:")
            loadData()
        }
    
        fun loadData() {
            Debug.d(TAG, "loadData: first_step")
            var dispose = Single.just(true).subscribe{it -> Debug.d(TAG, "single_result = $it")}
            dispose = Single.just(false).subscribe{it -> Debug.d(TAG, "single_result = $it")}
        }

And here logcat:

FilmsRxJavaViewModel(12149): init:
FilmsRxJavaViewModel(12149): loadData: first_step
FilmsRxJavaViewModel(12149): single_result = true
FilmsRxJavaViewModel(12149): single_result = false

As you can see the result of Single.just() single_result print twice. Because I twice call Single.just()

Nice. It’s correct.

Now I want to print result of Single.just() in my Activity:

In my Activity:

import androidx.lifecycle.ViewModelProviders
class FilmsRxJavaActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FilmsRxJavaActivityBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        init()
    }

    private fun init() {

  val viewModelProvider = ViewModelProviders.of(this)
        filmsRxJavaViewModel = viewModelProvider.get(FilmsRxJavaViewModel::class.java)
        var dispose = filmsRxJavaViewModel.isShowProgressSingle
            ?.subscribe { it ->
                Debug.d(TAG, "initLogic: isShowProgress = $it")
            }
    }

In my ViewModel

  import android.app.Application
  import android.view.View
  import androidx.lifecycle.AndroidViewModel
  import io.reactivex.Single
    
   class FilmsRxJavaViewModel(application: Application) : AndroidViewModel(application) {

var isShowProgressSingle: Single<Boolean>? = null
    init {
        Debug.d(TAG, "init:")
        loadData()
    }

    fun loadData() {
        Debug.d(TAG, "loadData: first_step")
        isShowProgressSingle = Single.just(true)
        Debug.d(TAG, "loadData: second_step")
        isShowProgressSingle = Single.just(false)
    }

And here logcat:

FilmsRxJavaViewModel(12311): init:
FilmsRxJavaViewModel(12311): loadData: first_step
FilmsRxJavaViewModel(12311): loadData: second_step

FilmsRxJavaActivity(12311): initLogic: isShowProgress = false

As you can see in my Activity the isShowProgress = false.

So it’s print only once. Why not print this (twice):

FilmsRxJavaActivity(12311): initLogic: isShowProgress = true
 FilmsRxJavaActivity(12311): initLogic: isShowProgress = false

?

READ  [ANSWERED] Android studio device detected but not reconized as an android device (ADB trouble)
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

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

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress