How to get a list of the activity history stack?

后端 未结 4 1587
北海茫月
北海茫月 2020-12-08 10:13

I\'m struggling with an issue in my app. I\'d like to provide a way to list the history of the previous opened activities.

I think there are two potential solutions

相关标签:
4条回答
  • 2020-12-08 10:45

    So is there a way to do that using a function from the SDK?

    No, sorry.

    Would it be possible to listen to "start/finish activity" events with some kind of receiver to a specific app (mine) and keep track of the stack history?

    You can use registerActivityLifecycleCallbacks() on Application, if you are only going to support API Level 14 and higher (a.k.a., Android 4.0+).

    0 讨论(0)
  • 2020-12-08 10:52

    you can use the following method:

    ActivityManager am = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
    
    int sizeStack =  am.getRunningTasks(2).size();
    
    for(int i = 0;i < sizeStack;i++){
    
        ComponentName cn = am.getRunningTasks(2).get(i).topActivity;
        Log.d("", cn.getClassName());
    }
    

    get a list of the activity stack.

    0 讨论(0)
  • 2020-12-08 11:00

    A very simple way to achieve Activity History is with classes name

    define activity_history_list in your mainActivity

    public static List<String> activity_history_list;
    

    and in every onStart() of every activity you create (for example MainActivity)

     @Override
        public void onStart() {
            super.onStart();
    
            activity_history_list.add(MainActivity.this.getClass().getSimpleName());
        }
    

    in a another activity (for example SettingActivity)

     @Override
            public void onStart() {
                super.onStart();
    
                MainActivity.activity_history_list.add(SettingActivity.this.getClass().getSimpleName());
            }
    

    You can access activity_history_list anywhere as long as it's static, you can print all history at once or access a specific activity in history by index

    0 讨论(0)
  • 2020-12-08 11:02

    Using ADB commands:

    1. adb shell dumpsys activity activities  -> displays list of activities in back stack
    
    2. adb shell dumpsys activity process  -> displays list process in back stack
    
    3. adb shell dumpsys activity intents  -> displays list of pending intents in back stack
    
    4. adb shell dumpsys activity broadcast  -> displays list of broadcast in back stack
    
    5. adb shell dumpsys activity services  -> displays list of services running in back stack
    

    Using Activity manager:

     ActivityManager m = (ActivityManager) ctx.getSystemService( ctx.ACTIVITY_SERVICE );
        List<RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(10);
        Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
        while(itr.hasNext()){
            RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
            int id = runningTaskInfo.id;
            CharSequence desc= runningTaskInfo.description;
            int numOfActivities = runningTaskInfo.numActivities;
            String topActivity = runningTaskInfo.topActivity.getShortClassName();
    }
    
    0 讨论(0)
提交回复
热议问题