Search code examples
androidandroid-fragmentsandroid-dialogfragment

DialogFragment is not Resumed after pressing Home Button


I have this code where a FragmentDialog opens at the top of the Main Activity. When i press home button and then again open the Fragment the onActivityCreated doesnt get called. Why is that?

Thank you

public class ProgressDialog extends DialogFragment {

private ProgressBar progress;
private TextView percentText;
private TextView descriptionText;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {


    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.download_screen,null);

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Please Wait....");
    builder.setView(v);
    builder.setNegativeButton("CANCEL",null);

    progress = v.findViewById(R.id.progressBar);
    percentText = (TextView) v.findViewById(R.id.percent);
    descriptionText = (TextView) v.findViewById(R.id.description);

    this.setCancelable(false);
    final AlertDialog dialog = builder.create();

    return dialog;
}

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

    if(savedInstanceState==null) {
        percentText.setText("Total Requests: 0");
        progress.setIndeterminate(true);
        descriptionText.setText("Analyzing Data....");
    }else{
        progress.setIndeterminate((boolean)savedInstanceState.get("indeterminate"));
        descriptionText.setText((String)savedInstanceState.get("description"));
        percentText.setText((String)savedInstanceState.get("percent"));
        progress.setMax((int)savedInstanceState.get("max"));
    }

}
}

Solution

  • See the fragment lifecycle from the below link.

    Fragment Lifecycle

    In this you can see that onActivityCreated() is called after onCreateView() and before onResume() of fragment.

    So when you minimise your fragment by pressing home button, it will call onPause() method.

    When you navigate back to your fragment, it will call onResume().