我有一個「hello world」-ish示例應用程序,它使用了android-support-v4 fragments API。 該活動由一個按鈕組成,點擊它將顯示一個DialogFragment。 但是,即使使用setRetainInstance(true),配置更改(如旋轉)也會導致對話框消失。儘管有setRetainInstance,但DialogFragment在旋轉時消失了(true)
任何想法如何解決這個問題?
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);
}
}
這似乎並不適用於我 - 對話仍然被駁回.. – ch3rryc0ke
@ ch3rryc0ke你有沒有找到任何解決方案,我有同樣的問題,我已經實現了上面的代碼,但對話框仍然被解僱 – drisse
我不確定這裏的解決方法,但是你是對的,你不應該在這種情況下使用'setRetainInstance' 。如果有狀態你想堅持,使用'savedInstanceState'或'arguments'' Bundle'。有一個[很好的例子](http://developer.android.com/reference/android/app/DialogFragme nt.html#AlertDialog)在Android文檔中介紹如何最好地使用DialogFragment來生成AlertDialog。 – jsadler