Search code examples
androidandroid-livedataandroid-viewmodel

Can't share Viewmodel between Activity and Fragment:


Android Studio 3.6

Here my viewModel:

import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.*
class BluetoothPageViewModel(application: Application) : AndroidViewModel(application) {
    private val isSearchingTableModeLiveData = MutableLiveData<Boolean>()
    private val isInitModeLiveData = MutableLiveData<Boolean>()
    private val errorMessageLiveData = MutableLiveData<String>()
private val toastMessageLiveData = MutableLiveData<String>()

 fun isInitModeLiveData(): LiveData<Boolean> {
        return isInitModeLiveData
    }

    fun isSearchingTableModeLiveData(): LiveData<Boolean> {
        return isSearchingTableModeLiveData
    }

    fun getErrorMessageLiveData(): LiveData<String> {
        return errorMessageLiveData
    }


    fun getToastMessageLiveData(): LiveData<String> {
        return toastMessageLiveData
    }

Here fragment the subscribe to this viewmodel and success call Observer.onChanged()

class BluetoothPageFragment : Fragment() {
private lateinit var bluetoothPageViewModel: BluetoothPageViewModel

 override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dataBinding =
            DataBindingUtil.inflate(inflater, R.layout.bluetooth_page_fragment, container, false)
        val view = dataBinding.getRoot()
        dataBinding.setHandler(this)
        init()
        return view
    }

    private fun init() {
        val context = this.context
        val viewViewModelProvider = ViewModelProviders.of(this)
        bluetoothPageViewModel = viewViewModelProvider.get(BluetoothPageViewModel::class.java)

        bluetoothPageViewModel.isInitModeLiveData().observe(this, // SUCCESS CALL
            Observer<Boolean> { isInitMode ->

            })
}

Here my activity the subscribe to this viewmodel and NOT call Observer.onChanged()

import androidx.lifecycle.ViewModelProviders
class QRBluetoothSwipeActivity : AppCompatActivity() {
private lateinit var bluetoothPageViewModel: BluetoothPageViewModel

 private fun init() {
        val viewViewModelProvider = ViewModelProviders.of(this)
        bluetoothPageViewModel = viewViewModelProvider.get(BluetoothPageViewModel::class.java)

        val customFragmentStateAdapter = CustomFragmentStateAdapter(this)
        customFragmentStateAdapter.addFragment(QrPageFragment())
        bluetoothPageFragment = BluetoothPageFragment()
        customFragmentStateAdapter.addFragment(bluetoothPageFragment)
        dataBinding.viewPager2.adapter = customFragmentStateAdapter

        initLogic()
    }

    private fun initLogic() {
        dataBinding.viewPager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                positionObservable.set(position)
            }
        })


         bluetoothPageViewModel.getToastMessageLiveData()  // this not call
            .observe(this,
                Observer<String> { message ->
                    Toast.makeText(this, message, Toast.LENGTH_LONG).show()
                })
}

Why not call getToastMessageLiveData() ?


Solution

  • In both cases you are using

    ViewModelProviders.of(this)
    

    It implifies you want this viewmodel with different scopes. One from Activity Scope and one from Fragment scope. If you want to share it. If you want to share viewmodel you have to use single scope. I recommend using scope of bigger element, in this case activity. In fragment you should call

    ViewModelProviders.of(activity)
    

    This should fix your issue.