How to know Activity Count of my application?
I am having multiple activities in my application. Please consider following case...
Activity A calls Acitivy B. B calls C. When user presses back key C generates notification and comes to B. Now again press back, so A will be displayed and Again back key will terminate A as well and notification is displayed... Now, when user presses notification icon Activity C gets loaded....Till this part behavior is correct....Now user presses back, I start activity A using my code as C was loaded from Notification.
Another case Activity A calls Acitivy B. B calls C. When user presses back key C generates notification and comes to B. Now user presses HOME KEY. Then after some time selects notification icon, it will display Activity C. Now user presses Back Key, By default it is displaying Activity B (which i want).... But my code(as C loaded from notification)... i m displaying activity A (n user presses back then Activity B is displayed).
I want to know Activity Count of my own application, so that i can avoid execution of my code to launch Activity A (if it is not 1).
So far i have done this using following code
ActivityManager actM = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> listm = actM.getRunningTasks(1);
int iNumActivity = listm.get(0).numActivities;
But usage of this code requires use permission GET_TASKS... I am 开发者_JAVA技巧not comfortable with this as i am not tracking any other application data/activity count.
How to fix this? Is there any other way to achieve this?
EDIT How to know activity count of own application?
One way is to push the shown Activity onto a stack (singleton) from onResume() in each of your Activities and pop it when 'Back' is pressed. That way, whenever 'Back' is pressed, you will know the current stack of Activities. As this will be shared by all the classes , it will help to store the values.
If just count is the issue..create a static count variable in a class say Constants . or may be three of them..so whenever you go to a particular class..increment the corresponding static public variable.
Edit: You can also keep the count of activities in the shared preferences.
精彩评论