I’ve searched not only stackoverflow, but another sites too, but I couldn’t find an answer to my problem. I’m starting now to learn Android, and I have a doubt about activities.
I have a MainActivity which creates a notification with two buttons. One is useless by now and the other (the Send button) will open an Alert with an EditText and a button to send the text to another app.
This is the code:
Intent mainActivityIntent = new Intent(MainActivity.this, NotificationActivity.class);
Intent shareActivityIntent = new Intent(MainActivity.this, ShareActivity.class);
PendingIntent mainActivityPIntent = PendingIntent.getActivity(MainActivity.this, 0, mainActivityIntent, 0);
PendingIntent shareActivityPIntent = PendingIntent.getActivity(MainActivity.this, 0, shareActivityIntent, 0);
Notification n = new Notification.InboxStyle
(new Notification.Builder(MainActivity.this)
.setContentTitle(getString(R.string.text_notification))
.setContentText(getString(R.string.text_notification))
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(mainActivityPIntent)
.addAction(R.drawable.logo, "Botão", mainActivityPIntent)
.addAction(R.drawable.ic_launcher, "Share", shareActivityPIntent)
)
.build();
n.flags |= Notification.FLAG_ONGOING_EVENT;
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, n);
As you can see, the Send button will open another Activity, which has the code below:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Mensagem");
alert.setMessage("Digite a mensagem");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Enviar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, value);
startActivity(Intent.createChooser(i, "Enviar"));
finishActivity(0);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
return;
}
});
alert.show();
This activity is responsible for opening the alert where the user will input the text and send to another application.
The situation is that when I call the activity it not only shows the alert but also open a new “screen”. Example: the notification is persistent, so it will be always showing on the notification drawer. If I’m on another app and I click the button on my notification, I want to open only the alert, but now it’s opening the alert AND the screen with the layout for my activity.
Is there a way to show only the alert, without showing the activity? Or do I have to use another method to do this?
Thank you very much.
If there’s no absolute need for an alert dialog, you can make the alert its own activity and just not use the alert builder. Put buttons and textviews or whatever you need as you would. Then in the manifest use
<activity android:theme="@android:style/Theme.Dialog">Here is more about themes. Hope this helps