Android 安卓从adb开始片段

Android 安卓从adb开始片段,android,android-fragments,adb,Android,Android Fragments,Adb,我想通过adb shell运行一个片段。这样做 adb shell am start -n com.example.myapplication/com.example.myapplication.MainActivity -e":android:show_fragment" com.example.myapplication.BlankFragment2 结果 Starting: Intent { cmp=com.example.myapplication/.MainAct

我想通过adb shell运行一个片段。这样做

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

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

但只有活动打开,没有片段。我做错了什么?

如果不使用adb编码,就无法打开片段。亚洲开发银行与该系统合作。活动是系统的一部分,但不是片段

要在onCreate方法中的活动中打开ADB中的片段,您需要选中Extra:

    val showFragment = intent.getStringExtra("show_fragment")
    if (showFragment != null) {
        if (showFragment == "StreamListFragment") {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, StreamListFragment())
                .commit()
        }
    }
容器-查看容器,您的片段将在其中沉淀

然后你就可以使用adb了

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

您在
MainActivity
中有代码来处理额外的活动吗?没有。我怎么做?在
onCreate()
中,调用
getIntent()
以获取用于创建活动的
Intent
。调用
getStringExtra()
检索额外值的值。使用该值确定要创建的
FragmentTransaction
和显示片段的
commit()
。请注意,从安全角度来看,将完全限定的类名作为
Intent
extra传递是一个糟糕的选择,特别是如果您只是盲目地显示该片段。几年前安卓系统中就有与此相关的安全漏洞,现在已经修复。谢谢你的回答。但这是打开碎片的唯一方法?我能不能像对待活动那样,只使用adb而不编写代码?“我能像对待活动那样,只使用adb而不编写代码?”——我当然不希望这样。如果可以,该应用程序的开发人员有一个安全漏洞。