Search code examples
androidandroid-dialogfragment

DialogFragment not showing, it opens and closes very quickly


I'm pretty new at Android and was given a bunch of a client's source code at work to fix. The DialogFragment I'm trying to create won't stay open. When I step through it, it opens and then closes again right away when newFragement.show() is called.

My onCreateDialog keeps telling me that my current targetSdk is 8, but I've changed it back and forth to 11 multiple times, even restarted Android Studio, but still tells me it's wrong.

So far all the other posts I've found related to this haven't answered my question. I haven't been able to get FragmentActivity to work. Here's my code:

ShowUpgradeDialogFragment class:

public class ShowUpgradeDialogFragment extends DialogFragment {

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setMessage(R.string.dialog_builder_message)
            .setPositiveButton(R.string.dialog_builder_positive, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // upgrade
                }
            })
            .setNegativeButton(R.string.dialog_builder_negative, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });//*/
    System.out.println ("hello");
    // Create the AlertDialog object and return it
    AlertDialog dialog = builder.create();
    return dialog;
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ila.tax_shield"
      android:versionCode="18" android:versionName="3.2.4">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18"/>
<fragment android:name=".ShowUpgradeDialogFragment"
              android:label="@string/app_name"
              android:windowIsFloating="true"
              android:background="@android:color/transparent"
              android:screenOrientation="portrait">
</fragment>

// remaining manifest

</manifest>

my onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DialogFragment newFragment = new ShowUpgradeDialogFragment();
    newFragment.show(getFragmentManager(), "dialog");

    // code

    finish() // wrong and was removed, which solved my bug
   }

display_update_message.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<fragment class="com.ila.tax_shield.fragments.ShowUpgradeDialogFragment"
          android:id="@+id/display_update_message"
          android:layout_weight="1"
          android:layout_height="match_parent"
          android:layout_width="2dp"
          tools:layout="@layout/display_update_message" />

</LinearLayout>

It doesn't like the 'android:name="com.ila.tax_shield.fragments.ShowUpgradeDialogFragment"', and I'm not understanding why

Thanks in advance for your help!


Solution

  • First thing you need to know is AndroidManifest is not the place where you declare your fragments. Take a look at the docs here for a list valid items on the Manifest file.

    Next, decide how you want to display ShowUpgradeDialogFragment. For a DialogFragment you have the below options

    • Embed it in using the layout xml.
    • Display it using the FragmentManager.

    Now, if you want to embed it then just have your fragment in the layout xml,

    OR Do this

    DialogFragment newFragment = new ShowUpgradeDialogFragment();
    newFragment.show(getFragmentManager(), "dialog");
    

    Doing both of the above at the same time makes no sense to me.