按“主页”按钮后未恢复DialogFragment

时间:2018-08-28 12:03:46

标签: android android-fragments android-dialogfragment

我有这段代码,其中FragmentDialog在Main Activity的顶部打开。当我按下主页按钮,然后再次打开Fragment时,不会调用onActivityCreated。为什么会这样?

谢谢

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"));
    }

}
}

1 个答案:

答案 0 :(得分:0)

从下面的链接中查看片段的生命周期。

Fragment Lifecycle

在此您可以看到onActivityCreated()在片段的onCreateView()之后和onResume()之前被调用。

因此,当您通过按下主屏幕按钮最小化片段时,它将调用onPause()方法。

当您导航回到片段时,它将调用onResume()