如何为Android应用程序设置默认活动

在Android中,您可以通过遵循“ AndroidManifest.xml ”中的“ intent-filter ”来配置应用程序的启动活动(默认活动)。

请参阅以下代码片段,将活动类“ logoActivity ”配置为默认活动。

档案:AndroidManifest.xml

<activity
            android:label="Logo"
            android:name=".logoActivity" >
             <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

例如,假设您有两个活动类,并且要将“ ListMobileActivity ”活动设置为应用程序的起始活动。

档案:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="List of Mobile OS"
            android:name=".ListMobileActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:label="List of Fruits"
            android:name=".ListFruitActivity" >
        </activity>
    </application>

</manifest>

另一方面,如果要将“ ListFruitActivity ”活动设置为开始活动,只需剪切并粘贴“ intent-filter ”,如下所示:

档案:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="List of Mobile OS"
            android:name=".ListMobileActivity" >
        </activity>
        <activity
            android:label="List of Fruits"
            android:name=".ListFruitActivity" >
             <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

翻译自: https://mkyong.com/android/how-to-set-default-activity-for-android-application/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值