Android adb命令获取当前Activity或者Fragment

用adb命令查看下面Demo处于NO.3 Fragment时的ActivityFragment信息。
在这里插入图片描述

查看当前Activity及其包名

adb shell "dumpsys window | grep mCurrentFocus"

输出结果如下:

mCurrentFocus=Window{b1deab4 u0 com.example.who/com.example.who.ui.activity.SecondActivity}

上面的com.example.who/com.example.who.ui.activity.SecondActivity}可以分成两部分看:/前的部分是包名,/后的部分是当前Activity的类全名。

查看全部窗口信息

adb shell dumpsys window windows |grep "Window #"

该命令会输出堆栈中全部的窗口信息。
输出结果如下,SecondActivity即为最上层的Activity

在这里插入图片描述

查看当前Fragment

adb shell dumpsys activity your.package.name > yourPathFragment.txt
可以先通过adb shell "dumpsys window | grep mCurrentFocus"查询出应用包名,再执行上面的命令即可。

打开Fragment.txt,我们可以在开头看到如下内容:

TASK com.example.who id=655 userId=0
  ACTIVITY com.example.who/.ui.activity.SecondActivity 49341c pid=4339
    Local Activity cb4da6d State:
      mResumed=true mStopped=false mFinished=false

其中cb4da6d是当前Activityhashcode。我们搜索它在Fragment.txt中的位置,可以看到如下内容。

	Line 3:     Local Activity cb4da6d State:
	Line 81:       context: com.example.who.ui.activity.SecondActivity@cb4da6d
	Line 82:       client: com.example.who.ui.activity.SecondActivity@cb4da6d (android.os.BinderProxy@292ac31)
	Line 105:     Local FragmentActivity cb4da6d State:

跳转到Local FragmentActivity cb4da6d State所在位置,然后继续往下看。可以看到Added Fragments。它列出了当前Fragment堆栈中的全部Fragment。

如果Demo第二页切换Fragment时使用add方法:

fragmentTransaction.add(R.id.container_fragment, mOneFragment, "OneFragmentTag");

fragmentTransaction.add(R.id.container_fragment, mTwoFragment, "TwoFragmentTag");

fragmentTransaction.add(R.id.container_fragment, mThreeFragment, "ThreeFragmentTag");

可以看到Added Fragments如下:

    Added Fragments:
      #0: OneFragment{c7ccdff (3e703195-7911-4c26-a36f-f14ee357c797) id=0x7f070055 OneFragmentTag}
      #1: TwoFragment{24e01a0 (b71c641a-6bf6-44d2-b01d-62f2c618136a) id=0x7f070055 TwoFragmentTag}
      #2: ThreeFragment{231b634 (d81580b3-ece9-48f7-b697-54f01c52a782) id=0x7f070055 ThreeFragmentTag}

如果Demo第二页切换Fragment时使用replace方法:

fragmentTransaction.replace(R.id.container_fragment, mOneFragment, "OneFragmentTag");

fragmentTransaction.replace(R.id.container_fragment, mTwoFragment, "TwoFragmentTag");

fragmentTransaction.replace(R.id.container_fragment, mThreeFragment, "ThreeFragmentTag");

可以看到Added Fragments如下:

    Added Fragments:
      #0: ThreeFragment{6e72f21 (f6e3fc39-c6e1-43ab-8914-a56c85425092) id=0x7f070055 ThreeFragmentTag}

如果在执行fragmentTransaction.commit();之前有执行addToBackStack方法,如下所示:

        fragmentTransaction.replace(R.id.container_fragment, mOneFragment, "OneFragmentTag");
        fragmentTransaction.addToBackStack("replaceOne");
        fragmentTransaction.commit();

那么我们还将看到Back StackBack Stack Indices,并从中看到回退栈的信息:

    Back Stack:
      #0: BackStackEntry{ce9e8a6 #0 replaceOne}
        mName=replaceOne mIndex=0 mCommitted=true
        Operations:
          Op #0: ADD OneFragment{3208bfc (09d07dcf-16fb-4e3d-84f2-87edc2de520d) id=0x7f070055 OneFragmentTag}
      #1: BackStackEntry{aded2e7 #1 replaceTwo}
        mName=replaceTwo mIndex=1 mCommitted=true
        Operations:
          Op #0: REMOVE OneFragment{3208bfc (09d07dcf-16fb-4e3d-84f2-87edc2de520d) id=0x7f070055 OneFragmentTag}
          Op #1: ADD TwoFragment{5fd0293 (5f60c66a-1171-4c6a-bbca-d43abb76bd02) id=0x7f070055 TwoFragmentTag}
      #2: BackStackEntry{acc5e94 #2 replaceThree}
        mName=replaceThree mIndex=2 mCommitted=true
        Operations:
          Op #0: REMOVE TwoFragment{5fd0293 (5f60c66a-1171-4c6a-bbca-d43abb76bd02) id=0x7f070055 TwoFragmentTag}
          Op #1: ADD ThreeFragment{de0310b (efd3eb6b-1e6a-466f-bb87-f039024911cd) id=0x7f070055 ThreeFragmentTag}
    Back Stack Indices:
      #0: BackStackEntry{ce9e8a6 #0 replaceOne}
      #1: BackStackEntry{aded2e7 #1 replaceTwo}
      #2: BackStackEntry{acc5e94 #2 replaceThree}