Search code examples
androidandroid-fragmentsonresumedismiss

Updating Activity with OnResume after Fragment Closes


I have a fragment that adds data to my database. When I close it with dismiss(), it returns to my activity. I want to then update my recyclerView in that activity.

My understanding of the activity lifecycle is that onResume should be called correct? I have a Log in my onResume method and as far as I can tell it is not being called.

What is the better solution, and why is this not being called?

onResume

  @Override
public void onResume() {
    super.onResume();
    Log.e("Resume", "Resuming");

}

The button click listener in my fragment. The Log here works perfectly fine.

  //save button for saving workouts
    mButton2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            String title = mEditText.getText().toString();
            String category = mSpinner.getSelectedItem().toString();
            String weight = mEditText2.getText().toString();
            int icon = buildIcon(category);

            //pass all the data into a new object for the Recycler View to store and show also refresh
            db.addWorkout(new Workout(title, weight, "8/8/8", icon));
            Log.e("Database Build", "Added a new workout: " + title);

            dismiss();
        }
    });

Solution

  • The Activity was never paused when you started dealing with the fragment. onResume won't get called in that scenario and that's expected lifecycle behavior.

    You should consider implementing some type of callback to let the Activity know when the Fragment has closed. The android documentation has a really good explanation of how to communicate with fragments. Use the pattern in the documentation and build yourself an OnFragmentClosedListener