I have a problem where I require to load the existing activity which is already openened by clicking on the activity, my problem is that when the notification is clicked a new activity is tried to be created and thus ca开发者_如何学编程lling the onCreate method and so fourth. How should I just only reopen the already opened (running in the background then) actiity?
Also how should I tackle the problem of Android might kill the background activity I intent to open with the notification?
Another big problem is if the notification is shown by the user manually goes to the app and does the required activities thus rendering the notification useless, and then when the user clicks on the notification how should I identify that the user already manually did the task the notification should do?
Regards, MilindaD
It sounds like there are a few separate problems here.
I have a problem where I require to load the existing activity which is already openened by clicking on the activity, my problem is that when the notification is clicked a new activity is tried to be created and thus calling the onCreate method and so fourth. How should I just only reopen the already opened (running in the background then) actiity?
So, if I understand you correctly, the user is viewing you activity, then they leave, and then they return via a notification. In this scenario, onCreate
would not normally be called again, unless the application killed itself, has android:noHistory
set, or was killed by the system to reclaim memory. If you are trying to write an activity that doesn't get killed, you are doing something wrong and you should probably be using a Service: http://developer.android.com/reference/android/app/Service.html
Also how should I tackle the problem of Android might kill the background activity I intent to open with the notification?
The problem here is with the words "background activity". You have to assume that any Activity might be killed if the user goes to do something else. If you need code to keep running regardless of what the user is doing, you should be using a service (see above).
Another big problem is if the notification is shown by the user manually goes to the app and does the required activities thus rendering the notification useless, and then when the user clicks on the notification how should I identify that the user already manually did the task the notification should do?
When your activity runs, it can simply cancel any notifications. Grab an instance of NotificationManager
through getSystemService()
and call .cancel(NOTIFICATION_ID)
.
Not sure if this has changed since 2011, very likely, but in 2016 altering the manifest worked very easily for me. It was adding the "singleTask" that made the difference.
<activity
android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
精彩评论