在使用层次结构查看器来减少层次结构时,我注意到在每次添加片段时(以"静态"或"动态"方式),片段总是包含在新的FrameLayout中.
这是一个例子:
这是我的活动布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="mainActivityRoot" >
<TextView
android:id="@+id/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:name="com.example.testfragments.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/hello_world" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是片段布局:
<ProgressBar android:id="@+id/ProgressBar1" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:contentDescription="mainFragmentRoot"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
活动源代码除了之外是空的setContentView,而片段源代码只包含
@Override
public View onCreateView(...) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
Run Code Online (Sandbox Code Playgroud)
现在,
我希望PrograssBar直接在活动根的层次结构中看到,但是有一个额外的FrameLayout,我不知道它来自哪里.这是一个屏幕截图,在黄色中绘制了额外的框架:

所以,我的问题是 - 它来自哪里?我可以摆脱它吗? 在我的实际应用程序中,这些额外的FrameLayouts正在创建非常深层次的层次结构,这可能对性能有害.
谢谢!
在 Kotlin 中,使用协程,假设我有一项工作是通过单击按钮开始的,并且在onStop调用活动之前不应结束。
看起来像这样的东西:
button.setOnClickListener {
CoroutineScope(...).launch{
print("Button clicked")
// How to wait for "onStop()" ?
print("Activity stopped")
}
}
Run Code Online (Sandbox Code Playgroud)
上述场景只是一般需要以函数调用 ( ) 的形式合并来自 SDK 内部的异步事件的示例onStop()。
应该怎么做呢?谢谢 :]