Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment disappears on rotation despite setRetainInstance(true)

Tags:

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);     } } 
like image 678
user1324237 Avatar asked Jul 03 '12 08:07

user1324237


People also ask

What is the difference between dialog & DialogFragment?

Show activity on this post. Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.

How do I start a DialogFragment?

Step 1: Create an Android Project with empty Activity. Step 2: Create a Fragment and Open your java and XML file add these codes. Step 3: Add this code to your Fragment XML file Source Code. Step 4: Add these code In your activity button click whare open DialogFragment.


2 Answers

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(); } 
like image 176
Tom anMoney Avatar answered Oct 02 '22 02:10

Tom anMoney


DialogFragments survive rotation changes on their own, u dont need to setRetainInstance

However if u set it, this is a reproducable bug with setRetainInstance(true) and DialogFragments

http://code.google.com/p/android/issues/detail?id=17423&q=setRetaininstance&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

just dont setRetainInstance, your dialog will work just fine

However if you have an AsyncTask running along in your DialogFragment and you want to persist it use LoaderManager with AsyncTaskLoader.. it also survives configuration changes

like image 34
AndroidGecko Avatar answered Oct 02 '22 03:10

AndroidGecko