Android Studio 3.6

这是我的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
    }


这里将订阅该视图模型和成功调用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 ->

            })
}


在这里,我的活动订阅该视图模型,而不调用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()
                })
}


为什么不致电getToastMessageLiveData()

最佳答案

在两种情况下,您都在使用

ViewModelProviders.of(this)


它暗示您希望此视图模型具有不同的作用域。一种来自活动范围,另一种来自片段范围。如果您要分享。
如果要共享视图模型,则必须使用单个作用域。我建议使用较大元素的范围,在这种情况下为活动。
片段中你应该打电话

ViewModelProviders.of(activity)


这应该可以解决您的问题。

07-28 04:24