0

I'm a newbie in Android developing and after reading the documentation about tasks and activities I can't get my application working correctly.

(First of all, sorry for my English)

My application consist of two activities: LOGIN and NEWS. Both activities launching method is singleTask.

The NEWS activity creates a notification with onCreate with the standard notification code of the Android notification tutorial!.

int icon = R.drawable.notification_icon;        // icon from resources
CharSequence tickerText = "Hello";              // ticker-text
long when = System.currentTimeMillis();         // notification time
Context context = getApplicationContext();      // application Context
CharSequence contentTitle = "My notification";  // expanded message title
CharSequence contentText = "Hello World!";      // expanded message text

Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

When I first open the application:

LOGIN --> onResume() --> NEWS --> onCreate() --> Notification

With the code

Intent newLogAct = new Intent(Login.this, News.class);
TomTuckerActivity.this.startActivity(newLogAct);

If I hit Back *NEWS* is destroyed and again:

LOGIN --> onResume() --> NEWS --> onCreate() --> Notification

(I don't like that loop, the reason of using it is explained at the end)

If I hit Home I go back to the main menu and here begin what I don't understand:

If I use the notification to relaunch the application there is no problem and NEWS window is opened again without calling onCreate and without sending the notification.

If I use the application icon when calling NEWS the singleTask option seems to be useless because onCreate() is called again and the notification is sent again.

What I want is to recover the application where I left it either I use the notification or the icon.

May a flag in the newLogAct will solve the problem?

Is OK tho have singleTask launching option in both activities?


About the Back button loop problem:

To avoid the loop I thought about using onCreate() instead of onResume(). However, when I relaunches the application with the application icon LOGIN is loaded but onCreate is not called so NEWS is not loaded.

Is there any other way to solve that?

Maybe with onNewIntent() method?

Octan
  • 348
  • 4
  • 19
  • First of all, why does the lauching method is singleTask? The [doc](http://developer.android.com/guide/topics/manifest/activity-element.html#lmode) says it should be used only in very specific cases. – Guillaume Brunerie Jul 13 '11 at 12:11
  • Because if not, when i hit Home and then reenter the app with the icon or the notification a new instance is created and then I have: Login - News - Login - News - ... Four activities instead of only two. Is there other way to do it? – Octan Jul 13 '11 at 12:22
  • If you reenter the app with the icon, I don’t think it will create a new instance, it should just come back to the last activity. – Guillaume Brunerie Jul 13 '11 at 17:16

1 Answers1

0

Your problem (or at least a part of it) seems to be: How to make a notification come back where you left the application.

Notification are not supposed to be used this way, clicking on a notification should start a new activity whose aim is to deal with the notification.

If you do want to come back to the application where you left it (I’m doing it), you can use the following trick (I don’t know if this is good practice, but it works and does not seems that hackish) : create an Activity called Autodestruct which call finish() in its onCreate() method, and make the notification run this Activity. This will restore the back stack with a dummy Activity on top of it and remove the dummy Activity immediately.

Guillaume Brunerie
  • 4,676
  • 3
  • 24
  • 32
  • Thanks for the answer. However i'm not having any problem when accessing from the notification, the application resumes where I left it. I changed singleTask method for singleTop as you suggested and it still works. When I use the icon to launch de application it creates a full new instance of it. But if I first use the notification to launch it, then I press the home button and then I use the icon the application is resumed where I left it. It's bit strange. – Octan Jul 13 '11 at 17:09
  • I did not suggested that, you should not even have to change the launch method, `standard` should work. – Guillaume Brunerie Jul 13 '11 at 17:14