Search code examples
androidandroid-fragmentspush-notificationis-empty

Fragment becomes empty after Notification


I have an Activity which loads a Fragment using getFragmentManager() . The fragment is working perfecty until an android notification appears. After that, the fragment becomes empty.

This is my application after the activity has loaded the fragment. The red square is the fragment.

fragment working ok

Here, a external notification arrives to the mobile:

fragment becomes blank

Only the fragment becomes empty:

Here my 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();
}
}

Here the 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>

And finally this is my 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);
        }
    }) ;
}

}

And the layout of my Fragment is:

    <?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>

Solution

  • Finally I found the solution. And ScrollView that has a layout_height="match_parent" when a notification is received changes his layout_height to 0.

    Changing layout_height="match_parent" to layout_height="wrap_content" in ScrollView solves the issue.