Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:37:50+00:00 2026-06-07T03:37:50+00:00

I started an activity with dialog theme from onReceive() method by using context variable.

  • 0

I started an activity with dialog theme from onReceive() method by using context variable. The started activity is having alert dialog layout with Ok and Cancel buttons. When user taps on Ok/Cancel button I am calling finish() method so the activity getting destroyed. But when I launch the application this activity is coming again. Is this because of starting the activity with context variable. Even if I set FLAG_ACTIVITY_NEW_TASK also it is coming again and again when i launch the application. Can someone please help me how can I avoid this activity.

EDIT

public class C2DMMessageReceiver extends BroadcastReceiver {

     @Override
    public void onReceive(Context context, Intent intent) {
           if ("com.google.android.c2dm.intent.RECEIVE".equals(intent.getAction())) {
                  Intent customDialogIntent = new Intent(context,DialogActivity.class);
                  customDialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(customDialogIntent);  
           }
    }

}


public class DialogActivity extends Activity{

        @Override
    protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.alert_dialog);
             TextView textAlertTitle = (TextView)findViewById(R.id.textAlertTitle);
             TextView textAlertMessage = (TextView)findViewById(R.id.textAlertMessage);
             Button button1Alert = (Button)findViewById(R.id.button1Alert);
             Button button2Alert = (Button)findViewById(R.id.button2Alert);

             textAlertTitle.setText("Notification");
             textAlertMessage.setText("One notification is waiting for you. Do you want to see the notification?");

              button1AlertOk.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                       Intent intent = new Intent(DialogActivity.this,HomeActivity.class)
                       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                       startActivity();
                  }
              });

    button2AlertCancel.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       finish();
                   }
            });
        }
}

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.ww.activity" android:versionCode="1" android:versionName="1.5">
<uses-sdk android:minSdkVersion="8" />
<permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.sample.ww.activity.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

<application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name="SplashActivity" android:theme="@android:style/Theme.NoTitleBar"
        android:screenOrientation="portrait">
        <intent-filter>
            <category android:name="android.intent.category.LAUNCHER"></category>
            <action android:name="android.intent.action.MAIN"></action>
        </intent-filter>
    </activity>
  <activity android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" android:name="HomeActivity"></activity>
  <activity android:theme="@style/AlertDialog.NoTitleBar" 
          android:screenOrientation="portrait" 
          android:name="DialogActivity"
          android:launchMode="singleInstance"
          android:taskAffinity="com.sample.ww.activity">
    </activity>
    <receiver android:name="com.nielsen.ww.receiver.C2DMMessageReceiver"
              android:permission="com.google.android.c2dm.permission.SEND">
             <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                <category android:name="com.sample.ww.activity" />
              </intent-filter>
              <intent-filter>
                  <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                  <category android:name="com.sample.ww.activity" />
              </intent-filter>
     </receiver>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:37:58+00:00Added an answer on June 7, 2026 at 3:37 am
    1. Don’t use launchMode="singleInstance". You don’t need that.

    2. When the user taps “OK” on your DialogActivity, you start the HomeActivity but you do not finish DialogActivity. That’s why, when you return to the application, you still see the DialogActivity. You need to call finish() after you call startActivity() in your onClick() method.

    3. You’ve got the taskAffinity set to “com.sample.ww.activity” for all of your activities (even if you don’t explicitly specify it, it defaults to the name of your package which is “com.sample.ww.activity”). If you want to have the activities run in different tasks then they need to have different taskAffinities. In any case, you may not need to have the activities running in different tasks if you fix the finish() problem in #2.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I create a Socket in my Service which is started from an Activity.
I have started Activity for result, but how to return string like parameter from
In my application i am trying to open a dialog box when activity started
I have a simple Activity that uses a android:theme=@android:style/Theme.Dialog in the manifest. My activity
I have an activity, that has in manifest: android:theme=@android:style/Theme.Dialog When I start it, it
When i'm calling i would like to have an activity to be started from
Hi I'm developing an app in which I'm using a dialog on an activity.
Possible Duplicate: Activity not started, its current task has been brought to the front
The purpose I aming for is, that 2 seconds after my activity was started
I have an Activity which shows a welcome message if started for the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.