2

I have created a custom notification, but when i pause the activity with the home button and a notification arrives, i press the notification and it creates a new activity and doesnt resume the previews activity and when i press the back button it goes to the previews one which is the same window. I have tried singleTop, singleTask, singleIntent, it works but it doesnt update the activity when a message enters, it is like the previews activity was in pause. how can i fix it?

if there is no solution i though in resuming the activity or destroy the paused activity or maybe restart the activity, is there a way to do this?

public void CustomNotification(String strtext) {
        // Using RemoteViews to bind custom layouts into Notification
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
                R.layout.customnotification);

        // Set Notification Title
        String strtitle = getString(R.string.customnotificationtitle);
        // Open NotificationView Class on Notification Click
        Intent intent = new Intent(this, NotificationView.class);
        // Send data to NotificationView Class
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);
        intent.putExtra("String T", "");
        //intent.putExtra("Integer C", 0);

        // Open NotificationView.java Activity
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                // Set Icon
                .setSmallIcon(R.drawable.ic_launcher)
                        // Set Ticker Message
                .setTicker(getString(R.string.customnotificationticker))
                        // Dismiss Notification
                .setAutoCancel(true)
                        // Set PendingIntent into Notification
                .setContentIntent(pIntent)
                        // Set RemoteViews into Notification
                .setContent(remoteViews);

        // Locate and set the Image into customnotificationtext.xml ImageViews
        remoteViews.setImageViewResource(R.id.imagenotileft,R.drawable.ic_launcher);
        remoteViews.setImageViewResource(R.id.imagenotiright,R.mipmap.ic_action_chat);

        // Locate and set the Text into customnotificationtext.xml TextViews
        remoteViews.setTextViewText(R.id.title,getString(R.string.customnotificationtitle));
        remoteViews.setTextViewText(R.id.text, strtext);

        // Create Notification Manager
        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Build Notification with Notification Manager
        notificationmanager.notify(0, builder.build());

    }

LOGS:

04-29 01:29:10.453  29001-29001/com.example.example E/STATE﹕NEW ACTIVITY
04-29 01:29:15.376  29501-29501/com.example.example D/Activity﹕ Activity.onPause(), editTextTapSensorList size: 0
04-29 01:30:06.981  29501-29501/com.example.example E/STATE﹕ RESUMING

WHEN I PRESS THE NOTIFICATION:

04-29 01:33:09.654  33449-33530/com.example.example E/STATE﹕NEW ACTIVITY

I will appreciate any answer, Thanks!

EDITED

ANSWER:

the answer was to add:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK);

because when i was in the same window and i pressed the home button, the activity was in paused, then if some notification arrived it brought me to that window but didnt updated the messages so I needed to create a new intent and erase the previous one which was in pause. so that code did the Job.

kobbycoder
  • 682
  • 2
  • 10
  • 33
  • Have you defined onResume() method in your activity? – keshav kowshik Apr 29 '15 at 05:23
  • @Keshav1234 i did , why? does it causes problem? or do i have to override it? – kobbycoder Apr 29 '15 at 05:24
  • If you want to perform some thing when your activity resumes, you definitely need to override onResume() method and add all your resume operations in that method. – keshav kowshik Apr 29 '15 at 05:25
  • @Keshav1234 but when i press the notification it is not resuming , it is creating a new activity thats why.... and the previews activity is still on resume – kobbycoder Apr 29 '15 at 05:27
  • If it is creating new activity then it seems that your activity is not getting paused, add log messages in onCreate and onResume and see which method is getting executed. – keshav kowshik Apr 29 '15 at 05:28
  • @Keshav1234, it is pausing just the first activity and resuming the first activity for first time, the second time i press the notification it creates a new one – kobbycoder Apr 29 '15 at 05:36
  • @Keshav1234 is there a way to detect if the activity is paused? if so then i could check that and go to that activity – kobbycoder Apr 29 '15 at 05:49
  • you should add log message in onPause and check.. – keshav kowshik Apr 29 '15 at 05:51

2 Answers2

4

To update your activity, you should override onNewIntent():

protected void onNewIntent (Intent intent) {
    super.onNewIntent(intent);
    //reload your data here
}

Android Docs say:

void onNewIntent (Intent intent)

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Note: as the docs say, you need to set android:launchMode="singleTop" in your Activity tag (in AndroidManifest).

Sufian
  • 6,405
  • 16
  • 66
  • 120
  • if i set single Top it will make my program works differently, wouldn't it take time to load it? i mean at least more milliseconds? and I have a function that creates a backup after the intent is destroyed(encrypted data) so i make it run on a thread. That's why i say my app would work differently. – kobbycoder May 04 '15 at 16:41
  • @kobbycoder sorry for the delayed response. Differently - yes. You may like to read [Android: Understanding Activity launch mode](http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode) to have a better understanding. – Sufian May 12 '15 at 08:21
2

try this in your service code to use notification :

 private static final int NOTIFY_ME_ID = 1222;
   NotificationManager mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

     private void Notifi(String message){
        int requestID = (int) System.currentTimeMillis();
                Intent intent = new Intent(Intent.ACTION_MAIN);
                //CLEAR PREVIOUS ACTIVITIES AND CREATE A NEW ONE
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(new ComponentName("packagenameofyourapplication",
                        "packagenameofyourapplication.NotificationView"));
                PendingIntent pintent = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                Notification notif = new Notification.Builder(this)
                        .setContentTitle(getString(R.string.notification_titel)).setContentText(message)
                        .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
                        .setContentIntent(pintent).build();
                notif.flags = Notification.FLAG_NO_CLEAR;
                mNotificationManager.notify(NOTIFY_ME_ID, notif);

}
kobbycoder
  • 682
  • 2
  • 10
  • 33
Anshuman
  • 308
  • 2
  • 6
  • Why do you feel this is an answer, how will this solve the issue of the question according to you? – keshav kowshik Apr 29 '15 at 05:43
  • i think @Anshuman didnt read, i have already tried, single task – kobbycoder Apr 29 '15 at 05:46
  • Actually after trying everything, this worked, as i said my notification was opening a new activity so Intent.FLAG_ACTIVITY_CLEAR_TOP erased previews intents which were on paused, which was good for my app i didnt want to resume the previous activities but creating a new one so Intent.FLAG_ACTIVITY_NEW_TASK) made the job. thak you i didn't understand at first, this has been 4 days of research and many hours of debugging. I am a beginner and there are many others, so next time when posting an answer please explain it. THANKS it helped me a lot – kobbycoder Apr 29 '15 at 18:42
  • @kobbycoder that is one way to solve this issue. But if you (or someone else) would like to reuse the previous instance while updating the few things only, my answer would come in handy. – Sufian Apr 30 '15 at 10:10