一、依赖
implementation("androidx.window:window:1.0.0-beta03")
implementation "androidx.startup:startup-runtime:1.0.0-alpha01"
二、xml静态注册
start up 初始化类:
class ExampleWindowInitializer : Initializer<SplitController> {
override fun dependencies(): MutableList<Class<out Initializer<*>>> {
return arrayListOf()
}
override fun create(context: Context): SplitController {
SplitController.initialize(context,R.xml.multi_window)
var splitRules = SplitController.getInstance().getSplitRules()
return SplitController.getInstance()
}
}
在AndroidManifest中静态注册
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
android:multiprocess="true"
tools:node="merge">
<meta-data
android:name="com.android.splitdemo.ExampleWindowInitializer"
android:value="androidx.startup" />
</provider>
在xml文件夹中创建multi_window.xml
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:window="http://schemas.android.com/apk/res-auto">
<SplitPairRule
window:splitRatio="0.5"
window:splitMinWidth="300dp"
window:finishPrimaryWithSecondary="true"
window:finishSecondaryWithPrimary="true"
window:clearTop="true">>
<SplitPairFilter
window:primaryActivityName="com.android.splitdemo.MainActivity"
window:secondaryActivityName="com.android.splitdemo.DetailActivity"/>
<SplitPairFilter
window:primaryActivityName="*"
window:secondaryActivityName="*/*"
window:secondaryActivityAction="android.intent.action.VIEW"/>
</SplitPairRule>
<SplitPlaceholderRule
window:placeholderActivityName="com.android.splitdemo.OtherActivity"
window:splitRatio="0.5"
window:splitMinWidth="300dp">
<ActivityFilter
window:activityName="com.android.splitdemo.MainActivity"/>
</SplitPlaceholderRule>
</resources>
二、动态注册
初始使用xml静态注册之外,还可以用代码实现动态注册
class MainActivity : AppCompatActivity() {
private var finishPrimaryWithSecondary: Boolean = true
private var finishSecondaryWithPrimary: Boolean = true
private var clearTop: Boolean = true
private var minWidth: Int = 300
private var minSmallestWidth: Int = 0
private var splitRatio: Float = 0.5f
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var splitController: SplitController = SplitController.getInstance()
val splitPairFilter: MutableSet<SplitPairFilter> = HashSet()
splitController.registerRule(
SplitPairRule(
splitPairFilter,
finishPrimaryWithSecondary,
finishSecondaryWithPrimary,
clearTop,
minWidth,
minSmallestWidth,
splitRatio,
LayoutDirection.LOCALE
),
)
val activityFilters: MutableSet<ActivityFilter> = HashSet()
addActivityFilter(activityFilters, MainActivity::class.java)
val intent = Intent(this@MainActivity, OtherActivity::class.java)
val placeholderRule = SplitPlaceholderRule(
activityFilters,
intent,
300,
0,
0.5f,
LayoutDirection.LOCALE,
)
splitController.registerRule(placeholderRule)
private fun addActivityFilter(
activityFilters: MutableSet<ActivityFilter>,
activityClass: Class<out Activity?>
) {
activityFilters.add(ActivityFilter(ComponentName(this@MainActivity, activityClass), null /* intentAction */))
}
}
[本章完...]