Home > Software engineering >  How can I list all opened activities on Xamarin.Android?
How can I list all opened activities on Xamarin.Android?

Time:10-04

I'm new to mobile development and I'm learning Xamarin. I already have some experience with C#. I'm currently studying hierarchical navigation with Xamarin.Android. In my project, I have the main activity, second and third activities where I navigate between them. I would like to know how can I list all opened activities? For example, if I'm in the main activity and I request the list of opened activities, the function should return me a list with the name of the main activity only, but if I'm in the third activity, the function should return me a list containing the name of the main, second and third activities, more or less like this:

MainActivity SecondActivity ThirdActivity

How can I get this information?

I am working with Xamarin.Android and not Xamarin.Forms.

CodePudding user response:

You could use ActivityManager to get the details of the Activities.

The GetRunningTasks would return a list of the tasks that are currently running. But this method was deprecated in API level 21. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.

For now, we only could use the BaseActivity to get the first activity and use the TopActivity to get the current one.

   ActivityManager m = (ActivityManager)this.GetSystemService(ActivityService);
             var runningTaskInfoList =  m.GetRunningTasks(10);
            foreach (var item in runningTaskInfoList)
            {                    
                string first = item.BaseActivity.ShortClassName;                   
                string current = item.TopActivity.ShortClassName;                 
                
            }  

CodePudding user response:

thanks for your help. I think that's almost it. I made a small modification to your code, looking like this:

 public string ListAllActivities()
    {
        string result = "";

        ActivityManager activityManager = (ActivityManager)this.GetSystemService(ActivityService);
        var runningTaskInfoList = activityManager.GetRunningTasks(10);
        foreach (var item in runningTaskInfoList)
        {
            string first = item.BaseActivity.ShortClassName.Substring((item.BaseActivity.ShortClassName.LastIndexOf('.')   1), item.BaseActivity.ShortClassName.Length - (item.BaseActivity.ShortClassName.LastIndexOf('.')   1));
            string current = item.TopActivity.ShortClassName.Substring((item.TopActivity.ShortClassName.LastIndexOf('.')   1), item.TopActivity.ShortClassName.Length - (item.TopActivity.ShortClassName.LastIndexOf('.')   1));
            result  = first   " - "   current   "\n";
        }

        return result;
    }

And the result is this:Activities Stack

When I'm in the third activity, I don't get the list for the second activity. If I, being in the third activity, could list the second and main activities, the issue would be resolved.

  • Related