gpt4 book ai didi

android - DialogFragment 在屏幕旋转时消失

转载 作者:行者123 更新时间:2023-11-29 21:05:10 24 4
gpt4 key购买 nike

尽管我将 RetainInstance 设置为 true,但这个相当简单的对话框在屏幕旋转后自行消失。任何想法怎么了?

public class StreetDialog extends DialogFragment {

public static StreetDialog newInstance(String[] values) {
StreetDialog f = new StreetDialog();
Bundle args = new Bundle();
args.putStringArray("values", values);
f.setArguments(args);
return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final String[] values = getArguments().getStringArray("values");

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//build my dialog
return builder.create();
}

@Override
public void onDestroyView() {
if (getDialog() != null && getRetainInstance())
getDialog().setDismissMessage(null);

super.onDestroyView();
}
}

最佳答案

如果我没记错的话是正常行为。我一般是给show方法提供一个tag,当Activity的onCreate再次被调用的时候,再去寻找这个tag。如果 fragment != null 我将其删除,然后再创建和显示新 fragment 。在代码中,我通常做的是:

Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
if (fragment != null) {
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
}
new CustomDialogFragment().show(getSupportFragmentManager(), FRAGMENT_TAG );

关于android - DialogFragment 在屏幕旋转时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24822367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com