Show Back Stack of Android

前端 未结 3 1359
执念已碎
执念已碎 2021-02-02 13:10

For better understanding the behavior of Android I\'d like to learn more about the back stack concept. Is there a way to list all activities as they are ordered in back stack. T

相关标签:
3条回答
  • 2021-02-02 13:42

    For the back stack of your own app, you can write your own solution using Application.ActivityLifecycleCallbacks:

    class MyApp : Application() {
       
       override fun onCreate() {
          super.onCreate()
          ActivityBackStackTracker.install(this)
       }
    }
    
    class ActivityBackStackTracker : Application.ActivityLifecycleCallbacks {
    
        override fun onActivityCreated(activity: Activity, bundle: Bundle?) {
            activityStack.add(activity::class)
        }
    
        override fun onActivityDestroyed(activity: Activity) {
            activityStack.remove(activity::class)
        }
    
        //..
    
        companion object {
            private val activityStack = mutableListOf<KClass<out Activity>>()
    
            fun getCurrentActivityStack() = listOf(activityStack)
    
            fun install(app: Application) {
                app.registerActivityLifecycleCallbacks(ActivityBackStackTracker())
            }
        }
    }
    

    Then at any moment you can log it with:

    Log.d(TAG, "${ActivityBackStackTracker.getCurrentActivityStack()})
    
    0 讨论(0)
  • 2021-02-02 13:46

    There is a question already which is similar to yours. I think this will answer your question:

    View the Task's activity stack

    0 讨论(0)
  • 2021-02-02 13:51

    I have found this information is available in Android Studio (0.5.1): View->Tool Windows->Android. Then on the left hand side select the System Information Icon and from it's drop down select 'Graphics State'. This will dump show a lot of information, but if you scroll down to 'View hierarchy:' you will see the current stack of views i.e. the 'Back Stack'.

    The OP did ask about running tasks, so instead if selecting 'Graphics State' select 'Activity Manager State' and you'll find more information (although I found it simpler to view the information in 'Graphics State' for specifically looking at what Activities are in the back stack).

    0 讨论(0)
提交回复
热议问题