通知后片段变空

Fragment becomes empty after Notification

我有一个 Activity 使用 getFragmentManager() 加载 Fragment 。在 android 通知出现之前,该片段工作正常。之后,片段变为空。

这是 activity 加载片段后我的应用程序。红色方块是片段。

此处,外部通知到达手机:

只有片段变空:

这是我的 Activity:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, FragmentSettings.OnFragmentInteractionListener{

FragmentManager fragmentManager;

    @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
if (fragmentManager == null) {
        fragmentManager = getSupportFragmentManager();

    }
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

        if (id == R.id.nav_locker_room) {
            fragmentNameSection = "FragmentLockerRoom";
            fragment = FragmentLockerRoom.newInstance(user, "");

        }
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment, fragmentNameSection).commit();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
}
@Override
public void onResume() {
    super.onResume();
}
}

这里是activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView

    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@drawable/side_nav_menu"
    app:itemTextColor="@android:color/white"
    app:itemIconTint="@android:color/white"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

最后这是我的 Fragment:

public class FragmentSettings extends Fragment {

private static final String ARG_USER = "user";
private static final String ARG_PARAM2 = "param2";


private String mParam1;
private String mParam2;
getUser user;

View v;
DatabaseManager db;
private OnFragmentInteractionListener mListener;
ToggleButton notificationTrain, notificationNextMatch, liveSounds;
LayoutInflater inflater;
ViewGroup container;
FragmentSettings fragmentSettings;

public FragmentSettings() {

}

public static FragmentSettings newInstance(getUser user, String param2) {
    FragmentSettings fragment = new FragmentSettings();
    Bundle args = new Bundle();
    args.putSerializable(ARG_USER, user);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        user = (com.androidsrc.futbolin.communications.http.auth.get.getUser) getArguments().getSerializable(ARG_USER);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    this.fragmentSettings = this;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    this.inflater = inflater;
    this.container = container;

    buildLayout();

    return v;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

public void buildLayout(){
    db = new DatabaseManager(getActivity());
    v = inflater.inflate(R.layout.fragment_fragment_settings, container, false);

    notificationTrain = v.findViewById(R.id.fragment_settings_notification_trainment_toggle);
    notificationNextMatch = v.findViewById(R.id.fragment_settings_notification_next_match_toggle);
    liveSounds = v.findViewById(R.id.fragment_settings_notification_live_sounds_toggle);

    notificationTrain.setChecked(db.findNotification().isTrainActive());
    notificationNextMatch.setChecked(db.findNotification().isMatchActive());
    liveSounds.setChecked(db.findNotification().isLiveSoundsActive());
    Log.e("notif",db.findNotification().toString());

    notificationTrain.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            if(db == null){
                db = new DatabaseManager(getActivity());
            }
            Notification notification = db.findNotification();
            notification.setTrainActive(isChecked);
            db.saveNotification(notification);
        }
    }) ;
    notificationNextMatch.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            if(db == null){
                db = new DatabaseManager(getActivity());
            }
            Notification notification = db.findNotification();
            notification.setMatchActive(isChecked);
            db.saveNotification(notification);
        }
    }) ;
    liveSounds.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            if(db == null){
                db = new DatabaseManager(getActivity());
            }
            Notification notification = db.findNotification();
            notification.setLiveSoundsActive(isChecked);
            db.saveNotification(notification);
        }
    }) ;
}

}

而我的 Fragment 的布局是:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">


        <ScrollView
            android:id="@+id/fragment_locker_room_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="60dp"
            >
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/fragment_locker_room_total_constraint_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
                    <!-- 
                        REST OF VIEWS
                    -->
        </androidx.constraintlayout.widget.ConstraintLayout>
        </ScrollView>
    </LinearLayout>

我终于找到了解决办法。 ScrollView 在收到通知时具有 layout_height="match_parent"layout_height 更改为 0。

ScrollView 中将 layout_height="match_parent" 更改为 layout_height="wrap_content" 解决了问题。