4

Am having an applications which contains activity A,B,C and activity A has been launched from the launcher and B from A and C from B. Now am having a button in C on clicking the button i should tell the position of the activity c in activity stack . Ex A-B->C means C is at the 3 position of the activity stack.. How could i find this ?

Thanks in advance.

evilone
  • 22,410
  • 7
  • 80
  • 107
Prasath
  • 1,121
  • 5
  • 15
  • 22

3 Answers3

1

Use ActivityManager to get the stack position of activity which u want

May use the following example code

ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );

List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);

to get the position of activity which u want use the below code for ur requirement

taskList.get(0).topActivity.getClassName()

and position start from o(zero).

Hope this will help you.

Karthikeyan Ve
  • 2,550
  • 4
  • 22
  • 40
  • This method is deprecated, and the docs contain the following warning: ````This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here. Such uses are not supported, and will likely break in the future```` – Zach Feb 19 '17 at 20:10
1

There is no other way to know this, because of how the Tasks are designed. See this and this So a solution could be to keep track of the activities by storing them in an array of activities. I hope this helps.

raukodraug
  • 11,519
  • 4
  • 35
  • 37
0

This is possible.

What you need is an activity that extends Application. You also declare this in your manifest.

Android global variable

You then declare a variable within this class:

      public static int count = 0;

Then within each class you have

 @Override
 protected void onResume() {
    YourApplicationClass.count++;

 }

 @Override
 protected void onPause() {
   YourApplicationClass.count--;
 }

You then know what position you are at all the time.

System.out.println("Count is: "+YourApplicationClass.count);

Community
  • 1
  • 1
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Interesting idea. Wouldn't you need to use `onStart` and `onStop` though, since `onPause` and `onResume` would be called when you, for example displayed a `Dialog`. – techi.services Feb 18 '11 at 17:44
  • I suppose it would depend on your app implementation. As for tab content onPause would be needed. But does show it's possible – Blundell Feb 18 '11 at 18:20