Q: Livedata observe does not see view I have the following activity structue and I want to observe live data and according to the data, I want to change the fragment. The first add fragment call works fine but those under the observe block do not because it does see activity_login view unset so it cannot find framelayout inside it. Where should I observe live data assuring view is set. I tried onViewCreate etc. but could not find a way.
public class LoginActivity extends AppCompatActivity {
private LoginViewModel loginViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    loginViewModel = ViewModelProviders.of(this, new LoginViewModelFactory(getApplication(),
LoginRepository.getInstance())).get(LoginViewModel.class);

    FragmentUtil.addFragment(getSupportFragmentManager(), R.id.entry_frame, new LoginFragment(), FragmentTag.LOGIN_FRAGMENT_TAG);

    loginViewModel.getCurrentFragmentEnum().observe(this, (currentFragmentEnum) -> {
if (currentFragmentEnum == LoginActivityFragments.REGISTER_FRAGMENT) {
FragmentUtil.addFragment(Objects.requireNonNull(getSupportFragmentManager()), R.id.entry_frame, new RegisterFragment(), FragmentTag.REGISTER_FRAGMENT_TAG);
} else if (currentFragmentEnum == LoginActivityFragments.LOGIN_FRAGMENT) {
FragmentUtil.addFragment(Objects.requireNonNull(getSupportFragmentManager()), R.id.entry_frame, new LoginFragment(), FragmentTag.LOGIN_FRAGMENT_TAG);
}
    });
}
}

LoginFragment
public class LoginFragment extends Fragment {
private LoginViewModel loginViewModel;

public LoginFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_login, container, false);
    FragmentLoginBinding fragmentLoginBinding = DataBindingUtil.setContentView(getActivity(), R.layout.fragment_login);
    loginViewModel = ViewModelProviders.of(this, new LoginViewModelFactory(getActivity().getApplication(),
LoginRepository.getInstance())).get(LoginViewModel.class);
    fragmentLoginBinding.setViewModel(loginViewModel);
    fragmentLoginBinding.executePendingBindings();
    return view;
}
}