按主页按钮后 DialogFragment 未恢复
DialogFragment is not Resumed after pressing Home Button
我有这段代码,其中 FragmentDialog 在 Main Activity 的顶部打开。当我按下主页按钮然后再次打开片段时,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"));
}
}
}
查看下面的片段生命周期link。
在此您可以看到 onActivityCreated()
在片段的 onCreateView()
之后和 onResume()
之前被调用。
所以当你按主页按钮最小化你的片段时,它会调用 onPause()
方法。
当您导航回您的片段时,它会调用 onResume()
。
我有这段代码,其中 FragmentDialog 在 Main Activity 的顶部打开。当我按下主页按钮然后再次打开片段时,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"));
}
}
}
查看下面的片段生命周期link。
在此您可以看到 onActivityCreated()
在片段的 onCreateView()
之后和 onResume()
之前被调用。
所以当你按主页按钮最小化你的片段时,它会调用 onPause()
方法。
当您导航回您的片段时,它会调用 onResume()
。