2

I know there are some examples on how to pass data from a activity to a dialog (i.e. Bundle or Intent). However, everything I've tried doesn't work. I keep getting NPE's and the "Unable to find explicit activity class" error. Even when I build a superbasic activity with a dialog it doesn't work. What do I have to put in the code to make it work?

MainActivity:

public class MainActivity extends AppCompatActivity {

  Button button;
  String textIWantToSee;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);

    textIWantToSee = "If this is the text I want to pass form this activity to the Fragment";

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });
  }
}

Dialog:

public class Dialog extends DialogFragment {

  TextView textView;

  @Override
  public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.dialog_layout, null);

    textView = (TextView) v.findViewById(R.id.textView);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);

    return builder.create();
  }
}

Update

MainActivity:

public class MainActivity extends AppCompatActivity {

TextView tvIntent;
Button button;
String textIWantToSee;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvIntent = (TextView) findViewById(R.id.tvIntent);
    button = (Button) findViewById(R.id.button);

    textIWantToSee = "If this is the text I want to pass form this activity to the Fragment";
    tvIntent.setText(textIWantToSee);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String intent = String.valueOf(tvIntent);
            Intent intentToDialog = new Intent(MainActivity.this, Dialog.class);
            intentToDialog.putExtra("keyForIntent", intent);
            startActivity(intentToDialog); //Here is the exception

        }
    });
}

} Dialog:

public class Dialog extends DialogFragment {

TextView textView;
String intent;

@Override
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.dialog_layout, null);

    textView = (TextView) v.findViewById(R.id.textView);
    Intent intentFromDialog = new Intent(getActivity().getApplicationContext(),MainActivity.class);
    intent = intentFromDialog.getStringExtra("keyForIntent");
    textView.setText(intent);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);

    return builder.create();
}

}

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: nl.blogvandetoekomst.passdatafromactivitytodialog, PID: 2947
              android.content.ActivityNotFoundException: Unable to find explicit activity class {nl.blogvandetoekomst.passdatafromactivitytodialog/nl.blogvandetoekomst.passdatafromactivitytodialog.Dialog}; have you declared this activity in your AndroidManifest.xml?
                  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
                  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
                  at android.app.Activity.startActivityForResult(Activity.java:3917)
                  at android.app.Activity.startActivityForResult(Activity.java:3877)
                  at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
                  at android.app.Activity.startActivity(Activity.java:4200)
                  at android.app.Activity.startActivity(Activity.java:4168)
                  at nl.blogvandetoekomst.passdatafromactivitytodialog.MainActivity$1.onClick(MainActivity.java:36)
                  at android.view.View.performClick(View.java:5198)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.blogvandetoekomst.passdatafromactivitytodialog">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

5 Answers5

14

This is how you pass data to a DialogFragment from an Activity :

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button button;
String textIWantToSee;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.button);

    textIWantToSee = "If this is the text I want to pass form this activity to the Fragment";

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Dialog dialogFragment = new Dialog();
            Bundle bundle = new Bundle();
            bundle.putString("TEXT",textIWantToSee);
            dialogFragment.setArguments(bundle);
            dialogFragment.show((MainActivity.this).getSupportFragmentManager(),"Image Dialog");

        }
    });
}
}

Dialog.java

public class Dialog extends DialogFragment {

TextView textView;

@Override
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.dialog_layout, null);

    Bundle bundle = getArguments();
    String imageLink = bundle.getString("TEXT","");

    textView = (TextView) v.findViewById(R.id.textView);

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v);

    return builder.create();
}
}
OBX
  • 6,044
  • 7
  • 33
  • 77
2

Your dialog fragment needs a hosting activity, It needs to be Inflated inside an Activity.

To inflate/show the DialogFragment in your MainActivity use:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Bundle bundle = new Bundle(); //Bundle containing data you are passing to the dialog
        bundle.putString("text", textIWantToSee);

        Dialog dialog = new Dialog(); //Create a new Dialog
        dialog.setArguments(bundle);

        dialog.show(getSupportFragmentManager(), "MY_DIALOG_TAG"); //Inflate the dialog
    }
});
thinkermsandi
  • 21
  • 1
  • 3
1

If you're using the support library you should use android.support.v4.app.DialogFragment instead of android.app.Dialog. Also, could you post the part of the code you're using to show the dialog?

Then you must be using the framework version of DialogFragment, as OBX has pointed:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button button;
    String textIWantToSee;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button); 

        textIWantToSee = "If this is the text I want to pass form this activity to the Fragment";

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Dialog dialogFragment = new Dialog();
                Bundle bundle = new Bundle();
                bundle.putString("TEXT",textIWantToSee);
                dialogFragment.setArguments(bundle);
                dialogFragment.show(getFragmentManager(),"Image Dialog");
            }
        });
    }
}

Dialog.java

public class Dialog extends DialogFragment {

    TextView textView;

    @Override
    public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.dialog_layout, null);

        Bundle bundle = getArguments();
        String imageLink = bundle.getString("TEXT","");

        textView = (TextView) v.findViewById(R.id.textView);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(v);
        return builder.create();
    }
}

This way is working perfectly fine for me.

EDITED:

When you do this in onClick:

Intent intentToDialog = new Intent(MainActivity.this, Dialog.class);
intentToDialog.putExtra("keyForIntent", intent);
startActivity(intentToDialog); //Here is the exception

You're not starting an activity, Dialog.class is an extended DialogFragment, you can not show a dialog starting an Activity. Try the last code I've posted.

krlos77
  • 331
  • 5
  • 13
  • When I import it manually "import android.support.v4.app.FragmentManager;" stays greyed out. I don't have a specific code to show the dialog. I tried both Bundle as well as Intent. Something like: "startActivity(intentToDialog);" and "new Dialog().show(getFragmentManager(),"dialog");. – Peter Alexander Christiaans Feb 04 '17 at 17:05
0

Use newInstance of Dialog fragment. refer : https://developer.android.com/reference/android/app/DialogFragment.html

   static MyDialogFragment newInstance(int num) {
    MyDialogFragment f = new MyDialogFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

If you are using kotlin, try defining the newInstance inside the companion object

companion object {
        fun newInstance(num: Int): MyDialogFragment{
            val f = DimensionDialogFragment()
            val args = Bundle()
            args.putInt("num", num)
            f.setArguments(args)

            return f
        }
    }
Deep P
  • 557
  • 5
  • 12
  • The OP said How to pass data from "Activity" to DialogFragment NOT FragmentActivity to DialogActivity. – olajide Jul 21 '18 at 22:57
-7

You can create the Dialog directly into your Activity:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        View v = inflater.inflate(R.layout.dialog_layout, null);

        TextView textView = (TextView) v.findViewById(R.id.textView);
        textView.setText(textIWantToSee);

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setView(v);
        builder.show();

        }
    });