本文介绍了尽管setRetainInstance上旋转DialogFragment消失(真)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Hello World-ish样本采用了Android支持-V4片段API的应用程序。该活动由一个按钮,点击它会显示一个DialogFragment。然而,像旋转的配置更改导致的对话框消失,即使setRetainInstance(真)被使用。

不知道如何解决这一问题?

RetFragment.java

 包me.local.HelloFroyo;

进口android.os.Bundle;
进口android.support.v4.app *。
进口android.util.Log;
进口android.view *。

公共类RetFragment扩展DialogFragment {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        setRetainInstance(真正的);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        返回inflater.inflate(R.layout.hello_dialog_fragment,集装箱);
    }

    @覆盖
    公共无效的onDestroy(){
        super.onDestroy();
        Log.e(RET,的onDestroy);
    }
}
 

MainActivity.java

 包me.local.HelloFroyo;

进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.view.View;

公共类MainActivity扩展FragmentActivity {

    私有静态最后弦乐TAG_DLG =myFragDlg;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
    }

    公共无效onShowClick(视图v){
        RetFragment RET =新RetFragment();
        ret.show(getSupportFragmentManager(),TAG_DLG);
    }
}
 

解决方案

只是为了对此有何评论:

即使setRetainInstance(真)被使用。

这不是即使,它的的缘故吧。如果setRetainInstance是假的,对话是旋转就好了。

这是工作围绕我发现在这里,这为我工作正常:<一href="http://$c$c.google.com/p/android/issues/detail?id=17423">http://$c$c.google.com/p/android/issues/detail?id=17423

  @覆盖
公共无效onDestroyView()
{
    对话框对话框= getDialog();

    围绕错误//工作:HTTP://$c$c.google.com/p/android/issues/detail ID = 17423
    如果((对话= NULL)及!&安培; getRetainInstance())
        dialog.setDismissMessage(空);

    super.onDestroyView();
}
 

I have a "hello world"-ish sample app that uses the android-support-v4 fragments API.The activity consists of a button, clicking it will show a DialogFragment.However, configuration changes like rotation cause the dialog to vanish, even if setRetainInstance(true) is used.

Any idea how to fix this?

RetFragment.java

package me.local.HelloFroyo;

import android.os.Bundle;
import android.support.v4.app.*;
import android.util.Log;
import android.view.*;

public class RetFragment extends DialogFragment {

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.hello_dialog_fragment, container);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("RET", "onDestroy");
    }
}

MainActivity.java

package me.local.HelloFroyo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;

public class MainActivity extends FragmentActivity {

    private static final String TAG_DLG = "myFragDlg";

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
    }

    public void onShowClick(View v) {
        RetFragment ret = new RetFragment();
        ret.show(getSupportFragmentManager(), TAG_DLG);
    }
}
解决方案

Just to comment on this:

"even if setRetainInstance(true) is used."

It's not "even if", it's "because of it." If setRetainInstance is false, the dialog is rotated just fine.

This is the work around I found here, which works fine for me: http://code.google.com/p/android/issues/detail?id=17423

@Override
public void onDestroyView()
{
    Dialog dialog = getDialog();

    // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
    if ((dialog != null) && getRetainInstance())
        dialog.setDismissMessage(null);

    super.onDestroyView();
}

这篇关于尽管setRetainInstance上旋转DialogFragment消失(真)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 22:51