Search code examples
androidandroid-fragmentsadb

Android. Start fragment from adb


I want to run a fragment via adb shell. Do so

adb shell am start -n com.example.myapplication/com.example.myapplication.MainActivity -e":android:show_fragment" com.example.myapplication.BlankFragment2

result

Starting: Intent { cmp=com.example.myapplication/.MainActivity (has extras) }

but only the activity opens without the fragment. What i do wrong?


Solution

  • You can't open Fragment without coding just using adb. ADB works with system. Activity is a part of system, but not Fragment.

    To open Fragment from ADB in your Activity in onCreate method you need to check Extra:

        val showFragment = intent.getStringExtra("show_fragment")
        if (showFragment != null) {
            if (showFragment == "StreamListFragment") {
                supportFragmentManager.beginTransaction()
                    .replace(R.id.container, StreamListFragment())
                    .commit()
            }
        }
    

    R.id.container - view container, where your fragment will settle.

    And after that you can to use adb.

    adb shell am start -n com.example.myapplication/com.example.myapplication.MainActivity --es "show_fragment" "StreamListFragment"