11

I am building an application which will show a notification and on clicking the notification, I want to open a transparent dialog like activity which will have option related to that notification.

I defined a transparent activity and registered it in notification like this.

 Intent notificationIntent = new Intent(context, EasyToDoReminder.class);
 PendingIntent contentIntent = PendingIntent
                                .getActivity(context, 0, notificationIntent, 0);

The activity starts, but the main activity is statred behind it. How to open the Reminder activity independently?

And I am not trying to show a dialog. I want to show an activity look like a dialog. I am already using the theme as you mentioned. I have also one main activity. When I am trying to start my transparent dialog like activity, the main activity is getting started then over that the dialog like activity is started.

Anyone can please suggest to do this in a better approach?

Arnab
  • 121
  • 1
  • 6
  • I'm having the same exact problem, every time I call the dialog activity my main activity opens behind it. I would love to see an answer to fix this – Peter Aug 15 '12 at 23:41

9 Answers9

3

ok i had this problem too
use another LAUNCHER activity for second activity

manifest :

    <activity android:name=".popup.PopUp"
              android:label="~.Popup"
              android:theme="@style/Theme.Transparent"
              android:launchMode="singleInstance"
              android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

and in intent :

        Intent i = new Intent();
        i.setClass(service.getBaseContext(), PopUp.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_SINGLE_TOP |
                Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        i.putExtra("id", id + "");
        context.startActivity(i);

if you call from a service use service.getBaseContext() , and if you not just use your context

Hamidreza Sadegh
  • 2,155
  • 31
  • 33
2

Here's what did the trick for me -

For the result intent do this:

public void onReceive(Context context, Intent intent) {
    ... 
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ...
}

For the activity encapsulating the dialog alert do this (in the manifest file):

     <activity
        android:launchMode="singleInstance"
    </activity>

Hope this helps...

verifier
  • 23
  • 4
1

You can't start your dialog without starting the activity. I think you need to do the following:

  • Create a new activity where you change its style to be a Dialog style:
    • android:theme="@android:style/Theme.Dialog"
  • In this new activity, add all what you want to happen when the user click the notification.
  • Link your notification to it like you did.

Now, when you click the notification, the dialog will show up.

iTurki
  • 16,292
  • 20
  • 87
  • 132
  • I have created new activity. And I am not trying to show a dialog. I want to show an activity look like a dialog. I am already using the theme as you mentioned. BI have also one main activity. When I am trying to start my transparent dialog like activity, the main activity is getting started then over that the dialog like activity is started. – Arnab Aug 14 '12 at 02:25
  • What if you kill the main activity and click the notification? Will it be behind the dialog also? – iTurki Aug 14 '12 at 02:27
1

Like booger said, Add android:theme="@android:style/ThemeDialog" to <application ... </application> part of your manifest to have your main activty just show as a dialog box, without a background.

You could also use an alert dialog with buttons in it which could activate the options you want , instead of having a second activity: AlertDialog reference

  • @Arnab "I have one main activity and one activity that looks like a dialog. I just what to start my dialog activity on its own without starting the main activity. Is it clearer now?" What is the main activity doing? Why do you need to have the two activities? Just trying to understand a little better. – Finding Nemo 2 is happening. Aug 14 '12 at 02:40
  • My main activity showing a list. For each item I can set a reminder. I want the reminder activity to some details like snooze, dismiss for that reminder. – Arnab Aug 14 '12 at 02:47
  • For your reminder button you could do this to launch the second activity that manages your reminders: `reminderbutton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { reminderfunction(); } }); private void reminderfunction { Intent remindermanager = new Intent(this, Secondactivity.class); startActivity(remindermanager); //launches second activity }` – Finding Nemo 2 is happening. Aug 14 '12 at 03:16
  • I am trying to do like this.. but i just want to start secondactivity from notification and the activity will be like a floating window (just like dialog) withou the main activity in the background – Arnab Aug 14 '12 at 03:34
  • 1
    [setFullScreenIntent](http://developer.android.com/reference/android/app/Notification.Builder.html#setFullScreenIntent%28android.app.PendingIntent,%20boolean%29) may be what you are looking for, it could open the second activity instead of going to the notification center. And the second activity could have the `android:theme="@android:style/Theme.Dialog"` property so it is just displayed as a floating window without a background. Sounds good in theory, trying to get a working example goin here. – Finding Nemo 2 is happening. Aug 14 '12 at 04:14
  • Hi, Just wanted to say im having the same problem where when i launch my dialog activity the main activity keeps launching behind it. it would be great to see a working answer – Peter Aug 15 '12 at 23:40
1

You need to call finish(); from your main activity so that it doesn't re-appear behind every other activity.

Better explained here Unexpected behavior when launching a dialog-style activity

Community
  • 1
  • 1
Peter
  • 5,071
  • 24
  • 79
  • 115
  • Thanks for your answer. But I am no where starting my activity before which I can finish() main activity. Because I just steeing my 2nd activity in notification manager. On tap it is starting the 2nd activity. I have not written a code like startActivity(). Can you tell me where I can write finish() ? – Arnab Aug 16 '12 at 19:48
  • This solution can be used I you explicitly call startActivity(). Before that finish() main activity. – Arnab Aug 16 '12 at 20:10
1

I solved my problem.

First I added the attribute in the matifest file for my 2nd activity like below.

android:launchMode="singleTask"

and while lauching from notification I set the following flag to the intent.

Intent notificationIntent = new Intent(context, EasyToDoReminder.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );

It works!!

Arnab
  • 121
  • 1
  • 6
0

I don't think you will be able to start a Dialog without first starting an Activity (which is I think what you are trying to do).

You could style your main activity to look like a dialog - but otherwise, I think you cannot start a dialog without having an Activity context to tie it to.

I may be mis-understanding your question.

Booger
  • 18,579
  • 7
  • 55
  • 72
  • I have one main activity and one activity that looks like a dialog. I just what to start my dialog activity on its own without starting the main activity. Is it clearer now? – Arnab Aug 14 '12 at 02:29
0

You may try Popup window, then inflate your layout inside that Popup window. Or, use android:theme="@android:style/Theme.Dialog" to get the effect of dialog when it's actually an activity

toantran
  • 1,789
  • 17
  • 25
0

Set android:taskAffinity and android:launchMode="singleTask" in your manifest file for your dialog activity. All credits to this answer.

Community
  • 1
  • 1
Oleksii Masnyi
  • 2,922
  • 2
  • 22
  • 27