サンプル

HomeFragment.java

					
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.navidrawact.R;
import com.example.navidrawact.databinding.FragmentHomeBinding;

public class HomeFragment extends Fragment {

	private HomeViewModel homeViewModel;
	private FragmentHomeBinding binding;

	public View onCreateView(@NonNull LayoutInflater inflater,
								ViewGroup container, Bundle savedInstanceState) {
		homeViewModel =
				new ViewModelProvider(this).get(HomeViewModel.class);

		binding = FragmentHomeBinding.inflate(inflater, container, false);
		View root = binding.getRoot();

		final TextView textView = binding.textHome;
		homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer() {
			@Override
			public void onChanged(@Nullable String s) {

				textView.setText("Hello World");
				Toast.makeText(getActivity(),	// Toastの表示
						"Hello World",
						Toast.LENGTH_LONG).show();
			}
		});
		return root;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		binding = null;
	}
}
				

HomeFlagmetでToastでHello Worldが表示されます。