I am creating notification using below code.
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Don't forget", System.currentTimeMillis());
notification.defaults|=Notification.DEFAULT_SOUND;
notification.defaults|=Notification.DEFAULT_LIGHTS;
notification.defaults|=Notification.DEFAULT_VIBRATE;
Intent intent2 = new Intent(Create_notification.this,After_alarm.class);
intent2.putExtra("arr", arr);
PendingIntent pintent = PendingIntent.getActivity(Create_notification.this, 0,intent2 ,0 );
nm.notify(i, notification);
I want to show some string on alert dialog after clicking on notifiction.For doing this I created Alert dialog in the oncreate() of the activity(i.e After_alarm activity) but this is showing the background of After_alarm activity and an alert dialog on it.I Only want to show alert dialog.Is their is a way to make an activity visible like alert dialog.I also changed theme of After_alarm of activity to alertdialog but dis is not wat I want.It is occupying a large space of screen.
Plz help me
thnx
As I said in the comment, you can make your activity transparent and show Alert Dialog on top of it. See answers How do I create a transparent Activity on Android? and how to make activities transparent for how to make it transparent. Hope it helps.
You could use .setVisibility(View.GONE) which will work fine.
alertdialog.setVisibility(View.GONE);
Related
So I have a notification which open an activity:
public void showNotification(View v) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.icon);
builder.setContentTitle("title text");
builder.setContentText("text");
builder.setOngoing(true);
Intent intent = new Intent(this, SecondClass.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SecondClass.class);
stackBuilder.addNextIntent(intent);
final PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
final NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NM.notify(0, builder.build());
The activity i made is a simple dialog styled activity. When i open the activity from the notification drawer, on top of any other app, like facebook, whatsapp, chrome browser and so on, the activity open as intended.
The problem is when i try to close it, and go back to the previous app.
When i click on the back button, to close my dialog styled activity, i go back straight to the phone's home screen, instead back to the previous app. Why?
When i click a "close" button i have created at the dialog:
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SecondClass.this.finish();
}
});
Same thing happening: the activity is finished, but i'm going back to the home screen, instead to the previous app. why??
*note: the previous app is still working at the background, even after i finish my activity.
While searching solutions i found this link:
How to open dialog styled activity from notification without previous activity closing?
but no solution was given.
P.S. I'm new to android developing, if possible, please make it simple :)
You looks like meet the same problem Regular Activity & Special Activity for Notification' Action, any My answer Here : After notification button return to original activity
After searching for long i found a solution at this link:
Go back to previous screen on backbutton pressed after responding to notification
apparently there were 2 problems in my code. First, as it was explained at the link, i did not use .addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
Second,according to: http://www.tutorialspoint.com/android/android_notifications.htm i shouldn't use a stack builder. A stack builder will contain an artificial back stack for the started Activity. This ensures that navigating backward from the Activity leads out of your application to the Home screen. And this is not what i wanted to achieve.
I wrote a dialog activity which can be triggered when clicking a notification, like picture1. But if launcher activity is in the stack it will appear below the dialog, just like picture2. If not, the dialog activity will show alone, which is what I want, like picture3.
Could someone tell me why?
The dialog activity uses the custom style like below:
<style name="DialogTransparent" parent="Theme.AppCompat.Light.Dialog">
<item name="windowNoTitle">true</item>
</style>
And below is the notification builder code:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setAutoCancel(false).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.todo_logo)).setSmallIcon(getNotificationIcon()).setContentText(getString(R.string.notificatioin_new_task)).setOngoing(true);
Intent alarmIntent = new Intent(this, DialogEventActivity.class);
PendingIntent alarmPendingIntent = PendingIntent.getActivity(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(alarmPendingIntent);
manager.notify(NEW_NOTIFICATION_ID, mBuilder.build());
I think the Activity's code is irrelevant, am I right?
If from Activity A to Activity B,and B is a subclass of DialogActivity or its theme is set to dialog,B will display before A as a dialog,A is visible also,If your application is not in foreground and you start Activity B,it will take your whole application to foreground,carry with your all activities in your app.So your Activity B and your last visible activity when leaved your app will be visible at the same time.
So to achieve your goal,you should let your Activity B as a normal activity,not a subclass of DialogActivity,and set background of your layout for this activity to transparent and set your contentView in center of screen.
answer is right here:
https://stackoverflow.com/a/8924417/5058310
really should google more before asking questions...
I Have a class that extends DialogFragment. I want to make my app do a Notification and when the user selects the notification, opens the DialogFragment with its layout and some data.
Is there are any source code to do this?
Hope anyone helps me. Thanks in advance.
To answer, you can't open a dialog via onclick of a notfication, there is no way in android, what you can do is,
to Start activity themed as Dialog
<activity android:theme="#android:style/Theme.Dialog">
now when startActivity() is called, its displayed like dialog, so moove everything you want to display here and call this activity by pending intent.
Also to add further, to make sure your activity wont appear in recent task list you can add the following excludeFromRecents=true
Just make the pendingintent open up one of your activities and have your activity be complete transparent and just open a dialog.
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
When a notification appears in the Android notification bar, the action for tapping on it is defined by the PendingIntent that was used while creating it, right?
But instead of launching an activity when the user taps on the notification, I would rather like to show an AlertDialog only. Is this possible?
The AlertDialog should ask: "Close notification" or "Show again in 1h" and so on.
Is there a way to show this AlertDialog only or do I have to launch an Activity, anyway?
You have to make an activity that does not set a contentView and just pops the Dialogue.
And remember to finish(); the activity when the dialogue is dimissed.
how can i show an AlertDialog in my activity, when the notification is clicked in notification area.
plz help....
i'm using multiple status bar notifications each with unique IDS.
I've solved a similar problem by having the notification intent open a new activity with a transparent background. The activity then spawns the AlertDialog.
Put some data to signal that an AlertDialog should be shown into a PendingIntent and put this into yourNotification.contentIntent of your notification. Display the AlertDialog from your Activity in case it receives this Intent.
Have a look at the Notifications Doc.