KaH*_*HeL 5 android android-dialogfragment
您好,我目前正在尝试实现一个教程对话框,其中一个对话框将在另一个对话框被关闭后出现。现在一切正常,但方向更改后,对话框会重新创建两次。更重要的是,在我关闭所有对话框然后尝试再次更改方向后,对话框将重新出现,这很烦人。到目前为止,这是我的代码:
public class TutorialOne extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_tutorial1, container, false);
Button next = (Button) view.findViewById(R.id.btn_next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
return view;
}
@Override
public void onDetach() {
super.onDetach();
if(isRemoving()) {
TutorialTwo tutorialTwo = new TutorialTwo();
tutorialTwo.show(getActivity().getSupportFragmentManager(), "tutorial_2");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
return dialog;
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?
改进
某些设备配置可能会在运行时发生变化(例如屏幕方向、键盘可用性和语言)。当发生此类更改时,Android 会重新启动正在运行的 Activity(调用 onDestroy(),然后调用 onCreate())。重新启动行为旨在通过使用与新设备配置匹配的替代资源自动重新加载应用程序,帮助您的应用程序适应新配置。
有两种方法可以处理这个问题
Allow your activity to restart when a configuration changes, but carry a stateful object to the new instance of your activity.
Run Code Online (Sandbox Code Playgroud)Prevent the system from restarting your activity during certain configuration changes, but receive a callback when the
configurations do change, so that you can manually update your
activity as necessary.
Run Code Online (Sandbox Code Playgroud)
根据两种方式,如果您不想重新创建活动并手动处理配置更改,则configchanges添加到项目清单下方
android:configChanges="方向|键盘隐藏"
现在,当这些配置之一发生更改时,MyActivity 不会重新启动。相反,MyActivity 会收到对 onConfigurationChanged() 的调用
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
Run Code Online (Sandbox Code Playgroud)
注意:处理运行时更改不建议注意:文档
| 归档时间: |
|
| 查看次数: |
4681 次 |
| 最近记录: |