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 9151901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:54:51+00:00 2026-06-17T11:54:51+00:00

So this application consists on one 2 activities. The first asks the user for

  • 0

So this application consists on one 2 activities. The first asks the user for specifications on making a notification. On the button click the activity creates the notification. The code for this activity is below:

package com.dewey.notifymanager;

import java.util.Random;


import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button) findViewById(R.id.button1);
        final TextView texty = (TextView) findViewById(R.id.textView2);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final EditText input2 = (EditText) findViewById(R.id.editText2);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                String notificationmessage = input.getText().toString();
                String notificationdetails = input2.getText().toString();
                texty.setText("Notification Created");
                displayNotification(notificationmessage, notificationdetails);
            }
        });
    }
    @Override
    protected void onStart() {

    super.onStart();


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }



    public void displayNotification(String msg,String details)
    {

         NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
         Notification notification = new Notification(R.drawable.ic_launcher, msg, System.currentTimeMillis());

        Random generator = new Random();
        int i = 90686958 - generator.nextInt(92938);


        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("details", details);
        intent.putExtra("msg", msg);
        intent.putExtra("id", i);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        notification.setLatestEventInfo(this, msg, details, pendingIntent);
        notification.flags = notification.FLAG_ONGOING_EVENT;
        manager.notify(i, notification);








    }

}

When the notification is clicked a new Activity is launched and some data is passed along in the intent, including the notification ID. The second activity then displays the data the notification contained, and it also has a button. When the user clicks this button the notification is removed. The code for the second activity is below:

package com.dewey.notifymanager;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ResultActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(null);
        setContentView(R.layout.activity_result);

        String details = getIntent().getExtras().getString("details");
        String msg = getIntent().getExtras().getString("msg");
        final int id = getIntent().getExtras().getInt("id");
        TextView title = (TextView)findViewById(R.id.title);
        TextView descrip = (TextView)findViewById(R.id.details);
        title.setText(msg);
        descrip.setText(details);

        Button exitButton = (Button)findViewById(R.id.exitButton);
        exitButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                NotificationManager notificationManager = (NotificationManager) 

              getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.cancel(id);
            finish();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_result, menu);
        return true;
    }
}

When I run the app it works oddly. When I create a notification and then click on it it launches the second activity and displays the correct data and the finish button rids the notification off the status bar. But then when I create a second notification and click on it, it launches the second activity, but displays the old data that the first application contained. I need to prevent this from happening.

I think so far that the problem lies with the fact that my activity is resuming savedInstanceState, but I set it to ‘null’ in the second activity. What exactly is my problem?

Is it even a problem regarding the activity resuming savedInstanceState or is it my logic?

Thanks for the help.

  • 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-17T11:54:52+00:00Added an answer on June 17, 2026 at 11:54 am

    You have to add the FLAG_ACTIVITY_NEW_TASK flag to your PendingIntent creation because the Activity launched from the Notification comes from outside the application context. So modify your code as follows:

    PendingIntent pendingIntent =
        PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    

    In addition, I noticed you are already using the support library, I would strongly recommend you move over to using Notification.Builder instead of using setLatestEventInfo(), which is a deprecated practice. The following code creates the same Notification, using the builder API from the support library.

    NotificationManager manager =
        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    
    Random generator = new Random();
    int i = 90686958 - generator.nextInt(92938);
    
    Intent intent = new Intent(this, ResultActivity.class);
    intent.putExtra("details", details);
    intent.putExtra("msg", msg);
    intent.putExtra("id", i);
    PendingIntent pendingIntent =
        PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setTicker(msg);
    builder.setSmallIcon(R.drawable.ic_launcher);
    
    builder.setContentTitle(msg);
    builder.setContentText(details);
    builder.setContentIntent(pendingIntent);
    
    Notification notification = builder.build();
    manager.notify(i, notification);
    

    As a side note, you might also consider using the TaskStackBuilder from the support library as well to construct your PendingIntent for you if you plan to implement any hierarchical navigation.

    EDIT

    Per your second question about creating more than one Notification at a time. First of all I feel compelled to say that this is not necessarily a good pattern; your application shouldn’t get in the habit of polluting the device’s window shade with multiple line items, condense them and your users will thank you.

    However, more to the point, the reason you can’t make it work is because a PendingIntent instance created with the same action, data, target, etc. as a previous instance will simply replace the first and not create a second one. One way you can workaround this is to use the same unique id that you are passing to NotificationManager and use it as the request code of the PendingIntent as well, i.e.:

    PendingIntent pendingIntent =
        PendingIntent.getActivity(this, i, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    

    This will make each PendingIntent you create unique enough to transmit the correct data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this case, the application consists of one or more Python files, plus a
The situation is this. I have an application written in vb.net. it consists or
Say we have an application which consists of one executable and 5 libraries. Regularly
I'm trying to write a test application that consists of a few fragments. One
My windows forms application consists of one Visual Studio solution and several projects. The
I have an application that consists of two processes, one client process with a
I am beginner in android. I have created one test application which consists of
I'm working on an application that consist of a couple of activities: Activity 1:
I'm building an application which (currently) consists of one web application (ASP.NET MVC) and
So I'm having this application that verifies weather a user can log-in or not.

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.