0

I have a MainActivity form that I am opening CreatePassword Activity in that, I am saving password and finish CreatePasswordActivity with sending Intent back to MainActivity.

Like MainActivity -----> CreatePassword(Finish) ---Intent----> MainActivity

  private fun observeIntentResult() {
        generatePasswordViewModel.getIntentResult().observe(this@CreatePasswordActivity, androidx.lifecycle.Observer { intent ->
            Toast.makeText(this, "Got Same Data", Toast.LENGTH_SHORT).show()
            setResult(Activity.RESULT_OK, intent)
            finish()
        })
    }

But Now when I open CreatePasswordActivity again from MainActivity, it's LiveData automatically sending previous data (intent) and CreatePasswordActivity is suddenly finished.

Do I have any mistake in implementing code? Anybody have idea how to resolve this?

Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150
  • Maybe you are calling this function in onCreate() :/ – moumenShobakey Mar 29 '20 at 04:51
  • Yes, I am calling from onCreate(), so where should I need to call? I have no idea more than this – Siddhpura Amit Mar 29 '20 at 04:59
  • Wrap it with a condition to make sure that it's the first time to be called , You can for example ,put a variable in the intent you open createPassword with from MainActivity , then Override onActivityResult and set its value from there to a member value in PasswordActivity and check its value in the Condition if it equals specific value then it's called from the intent so don't call this function – moumenShobakey Mar 29 '20 at 05:18

1 Answers1

0

Finally I have used below class, which helped me to resolve current issue, will see and update answer in future if I will be able to find any better solution.

open class VolatileLiveData<T> : MutableLiveData<T>() {
    private val lastValueSeq = AtomicInteger(0)
    private val wrappers = HashMap<Observer<in T>, Observer<T>>()

    @MainThread
    public override fun setValue(value: T) {
        lastValueSeq.incrementAndGet()
        super.setValue(value)
    }

    @MainThread
    public override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        val observerWrapper = ObserverWrapper(lastValueSeq, observer)
        wrappers[observer] = observerWrapper
        super.observe(owner, observerWrapper)
    }

    @MainThread
    public override fun observeForever(observer: Observer<in T>) {
        val observerWrapper = ObserverWrapper(lastValueSeq, observer)
        wrappers[observer] = observerWrapper
        super.observeForever(observerWrapper)
    }

    @MainThread
    public override fun removeObserver(observer: Observer<in T>) {
        val observerWrapper = wrappers[observer]
        observerWrapper?.let {
            wrappers.remove(observerWrapper)
            super.removeObserver(observerWrapper)
        }
    }
}

private class ObserverWrapper<T>(private var currentSeq: AtomicInteger, private val observer: Observer<in T>) : Observer<T> {
    private val initialSeq = currentSeq.get()
    private var _observer: Observer<in T> = Observer {
        if (currentSeq.get() != initialSeq) {
            // Optimization: this wrapper implementation is only needed in the beginning.
            // Once a valid call is made (i.e. with a different concurrent sequence), we
            // get rid of it any apply the real implementation as a direct callthrough.
            _observer = observer
            _observer.onChanged(it)
        }
    }

    override fun onChanged(value: T) {
        _observer.onChanged(value)
    }
}
Siddhpura Amit
  • 14,534
  • 16
  • 91
  • 150