onOptionsItemSelected 访问 dataBinding viewModel

onOptionsItemSelected access the dataBinding viewModel

如何让 dataBinding viewModel 在 onOptionsItemSelected 方法上可访问。

class TestFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // viewModel

        val viewModelFactory = TestViewModelFactory(
            ...
        )
        val viewModel = ViewModelProvider(
            this, viewModelFactory).get(TestViewModel::class.java)

        // dataBinding

        val binding = FragmentTestBinding.inflate(inflater)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel

        return binding.root
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when(item.itemId) {
            R.id.test_menu_item -> {
                this.findNavController().navigate(
                    TestFragmentDirections
                        .actionTestFragmentToAnotherTestFragment(
                            ...
                        )
                )
                viewModel.onNavigated() // How to access the viewModel here
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}

好的,声明整个全局变量都可以访问的lateinit全局变量class

private lateinit var viewModel : TestViewModel

现在在 onCreate() 中用工厂初始化 viewModel:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // factory
        val viewModelFactory = TestViewModelFactory()
        // viewModel
        viewModel = ViewModelProvider(this, viewModelFactory).get(TestViewModel::class.java)
    }

所以整个代码看起来像:

class TestFragment : Fragment() {

    private lateinit var viewModel : TestViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // factory
        val viewModelFactory = TestViewModelFactory()
        // viewModel
        viewModel = ViewModelProvider(this, viewModelFactory).get(TestViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // dataBinding
        val binding = FragmentTestBinding.inflate(inflater)
        binding.lifecycleOwner = this
        binding.viewModel = viewModel

        return binding.root
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when(item.itemId) {
            R.id.test_menu_item -> {
                this.findNavController().navigate(
                    TestFragmentDirections.actionTestFragmentToAnotherTestFragment()
                )
                viewModel.onNavigated() // How to access the viewModel here
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}