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() me开发者_运维知识库thod?
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.
精彩评论