Arnab
Arnab

Reputation: 121

Transparent activity is opening over main activity. How to open it not over main activity?

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?

Upvotes: 11

Views: 6962

Answers (9)

Oleksii Masnyi
Oleksii Masnyi

Reputation: 2912

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

Upvotes: 0

Hamidreza Sadegh
Hamidreza Sadegh

Reputation: 2196

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

Upvotes: 3

verifier
verifier

Reputation: 23

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...

Upvotes: 2

Arnab
Arnab

Reputation: 121

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!!

Upvotes: 1

Peter
Peter

Reputation: 5131

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

Upvotes: 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

Upvotes: 1

toantran
toantran

Reputation: 1819

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

Upvotes: 0

iTurki
iTurki

Reputation: 16398

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.

Upvotes: 1

Booger
Booger

Reputation: 18725

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.

Upvotes: 0

Related Questions