简体   繁体   中英

Android data binding makes fragment display full screen, breaks navigation

I have a single activity app with bottom navigation tabs using Android Architecture Navigation Component.

Here is the code for one of the fragments:

public class ProfileFragment extends BaseFragment {

    private final int layout = R.layout.fragment_profile;
    //private final int layout = R.layout.fragment_profile_;

    private ProfileViewModel mViewModel;
    //  FragmentProfileBinding mBinding;

    public static ProfileFragment newInstance() {
        return new ProfileFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(layout, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this, viewModelFactory).get(ProfileViewModel.class);

//        mBinding = DataBindingUtil.setContentView(getActivity(), layout);
//        mBinding.setLifecycleOwner(this);
//        mBinding.setVm(mViewModel);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ((mcApplication) getActivity().getApplication())
                .getApplicationComponent()
                .inject(this);
    }
}

It has 2 possible versions (one which is with data binding is commented).

Current version without databinding works correctly, but the version with data binding displays fragment fullscreen instead of displaying it within the androidx.navigation.fragment.NavHostFragment area of my MainActivity. Binding of data works, my problem is that fragment displays full screen and trying to programmatically navigate from this fragment does not work. So databinding in this case breaks things. What am I doing wrong?

Here is contents of the fragment_profile_.xml file:

    <?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >
    <data>
        <variable
            name="vm"
            type="com.synergy.megacampus3.viewmodel.ProfileViewModel" />
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
                <TextView
                    android:id="@+id/username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:text="@={vm.fullUserName}"
                    android:textColor="@color/mc_text_color_1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/avatar"
                    tools:textSize="16sp" />

    </LinearLayout>
</layout>

My problem was that I initialized binding in the wrong place and wrong way. The working code is this:

public static ProfileFragment newInstance() {
    return new ProfileFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ((mcApplication) getActivity().getApplication())
            .getApplicationComponent()
            .inject(this);
    mViewModel = ViewModelProviders.of(this, viewModelFactory).get(ProfileViewModel.class);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    mBinding = DataBindingUtil.inflate(
            inflater, layout, container, false);
    View view = mBinding.getRoot();
    mBinding.setLifecycleOwner(this);
    mBinding.setVm(mViewModel);

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

Code taken from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM