Android adb command to get the current Activity or Fragment

Use the adb command to view the NO.3 Fragmenttime Activityand Fragmentinformation of the demo below.
insert image description here

View the current Activity and its package name

adb shell "dumpsys window | grep mCurrentFocus"

The output is as follows:

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

The above com.example.who/com.example.who.ui.activity.SecondActivity}can be divided into two parts: /the first part is the package name, and /the latter part is Activitythe full name of the current class.

View all window information

adb shell dumpsys window windows |grep "Window #"

This command will output all window information in the stack.
The output is as follows, SecondActivitywhich is the top layer Activity:

insert image description here

View the current Fragment

adb shell dumpsys activity your.package.name > yourPath\Fragment.txt
You can query the application package name first adb shell "dumpsys window | grep mCurrentFocus", and then execute the above command.

Open Fragment.txt, we can see the following at the beginning:

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

where cb4da6dis Activitycurrent hashcode. We search for its Fragment.txtlocation in , and we can see the following.

	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:

Jump to Local FragmentActivity cb4da6d Statewhere you are, and read on. can see Added Fragments. It lists all Fragments in the current Fragment stack.

If the add method is used when switching Fragments on the second page of Demo:

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

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

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

It can be seen Added Fragmentsas follows:

    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}

If the replace method is used when switching Fragments on the second page of Demo:

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

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

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

It can be seen Added Fragmentsas follows:

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

If fragmentTransaction.commit();you have execute addToBackStackmethod before execute like this:

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

Then we will also see Back Stackand Back Stack Indices, and see the back stack information from it:

    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}

Guess you like

Origin blog.csdn.net/jiejingguo/article/details/128822242