通知后片段变为空

时间:2018-07-06 16:39:43

标签: android android-fragments push-notification is-empty

我有一个Activity,它使用Fragment加载getFragmentManager()。片段运行完美,直到出现Android通知。之后,片段变成空的。

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

fragment working ok

在这里,外部通知到达了手机:

fragment becomes blank

只有片段变为空:

这是我的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>

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案。收到通知的ScrollView的{​​{1}}的{​​{1}}更改为0。

layout_height="match_parent"中的layout_height更改为layout_height="match_parent"可解决此问题。