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