0

I have implemented C2DM in the application and everything seems to work properly when app is running. But in case app is not running and push message is received. On tap of push message in the notification panel a separate activity is called. This activity can be called PushMsgHandlerActivity. From this activity one can be redirected to other activity within the application. Say user clicks on a button in PushMsgHandlerActivity and is redirected to Activity A which is the launcher activity. From here, if user presses back button then would go to the view which was there before opening up the push message. If now the home button is kept pressed and we select the app from there then instead of starting launcher activity the app starts the PushMsgHandlerActivity again. The reason I suppose is since that's the activity that was started first for the application.

Anyways of starting launcher activity in this case.

Please note that this issue occurs only when push message is received and application is not running. Otherwise the app works and behaves properly.

sunil
  • 9,541
  • 18
  • 66
  • 88
  • did you find a solution for this problem? I am experiencing the same behavior and cant figure out how to solve it. Thank you – Thiago Dec 15 '11 at 19:09

1 Answers1

0

If you never want the user to be returned to the PushMsgHandlerActivity, try turning off history for that Activity, as described here, by adding the flag android:noHistory="true" to your manifest file in the section for that activity. This should only bring the user to that Activity when you take them there (through clicking a push message).

Here is a simple example that seems to do what you want. It has two activities, a NotifyMainActivity which immediately starts a notification, and a HandleNotificationActivity which is called when you click the notification.

NotifyMainActivity:

public class NotifyMainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);

        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";
        Intent notificationIntent = new Intent(this,
                HandleNotificationActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);

        mNotificationManager.notify(1, notification);
    }
}

HandleNotificationActivity:

public class HandleNotificationActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handler);
    }

    public void startMain(View view) {
        Intent i = new Intent(this, NotifyMainActivity.class);
        startActivity(i);
        finish();
    }
}

handler.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="startMain"
        android:text="Main" />
</LinearLayout>

I have tested by starting the main activity, which creates a notification. Then press the back button, go to your home screen. Click the notification, which starts the handler activity. Then click the Main button, and press back. It will not take you back to the handler since it called finish() on itself. I hope this example can clear things up a bit.

Community
  • 1
  • 1
skynet
  • 9,898
  • 5
  • 43
  • 52
  • Even after setting android:noHistory="true" in the manifest that activity gets called. – sunil Oct 24 '11 at 14:05
  • In that case try calling `finish` immediately after you call `startActivity` in your PushMsgHandlerActivity. This should remove that activity from the stack. Described [here](http://stackoverflow.com/questions/5001882/how-to-clear-specific-activity-from-the-stack-history). – skynet Oct 24 '11 at 14:31
  • I am finishing the activity too. I guess the issue is only when the application is not running and push notification comes up. This would start PushMsgHandlerActivity which in turn will start another activity of the app say Activity A. Now there is a home button in activity A when pressed would redirect to home activity. The root activity at this time is the Activity A but I want the root activity to be home activity all the times. I hope I have explained what is happening. – sunil Nov 02 '11 at 09:24
  • You could call `finish` when the user presses the home button, that way Activity A is no longer in the stack. – skynet Nov 04 '11 at 18:00