Android 更改应用程序的启动活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3631982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:05:03  来源:igfitidea点击:

Change application's starting activity

androidandroid-activity

提问by Kyle

I have created the meat and guts of my application but I want to add a different activity that will be the starting point (sort of a log-in screen).

我已经创建了我的应用程序的内容,但我想添加一个不同的活动作为起点(有点像登录屏幕)。

Couple questions:

几个问题:

  • 1 I have a fairly decent handle on how to switch between activities (based on this article: http://www.linux-mag.com/id/7498) but I'm not sure how to go about creating a new one (with eclipse).

  • 2 Once I have a new activity created, how can I set it as the default activity of my application?I presume I could just change the name of the classes...but is there a more elegant way to handle that (maybe within the AndroidManifest.xml)?

  • 1 我对如何在活动之间切换有一个相当不错的处理(基于这篇文章:http: //www.linux-mag.com/id/7498),但我不知道如何去创建一个新的(与日食)

  • 2 创建新活动后,如何将其设置为应用程序的默认活动?我想我可以只更改类的名称……但是有没有更优雅的方法来处理它(可能在AndroidManifest.xml.

回答by Nathan Schwermann

Yes, you use the AndroidManifest.xmlfile. You can actually even have more than one launcher activity specified in your application manifest. To make an activity seen on the launcher you add these attributes to your activity in the manifest:

是的,您使用该AndroidManifest.xml文件。实际上,您甚至可以在应用程序清单中指定多个启动器活动。要在启动器上看到活动,您可以将这些属性添加到清单中的活动:

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

回答by neeraj t

Go to AndroidManifest.xml in the root folder of your project and change the Activity name which you want to execute first.

转到项目根文件夹中的 AndroidManifest.xml 并更改要首先执行的活动名称。

Example:

例子:

<activity android:name=".put your started activity name here"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

回答by Zeezer

If you are using Android Studio and you might have previously selected another Activityto launch.

如果您使用的是 Android Studio,并且您之前可能已经选择了另一个要启动的Activity

Click on Run > Edit configurationand then make sure that Launch default Activityis selected.

单击Run > Edit configuration,然后确保选择了启动默认活动

Launch default Activity

启动默认活动

回答by LMK

 <application
    android:icon="@drawable/YOUR_ICON"    <!-- THIS ICON(IMAGE) WILL BE SHOWN IN YOUR APPS -->
    android:label="MY APP NAME " >    <!-- HERE LABEL(APP NAME) -->
    <activity
        android:name=".application's starting activity"  <!-- (.)dot means current dir, if your activity is in another package then give full package name ex: com.xxx.Activity  -->
        android:label="LABEL FOR ACTIVITY "
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

回答by Bhunnu Baba

Follow to below instructions:

请按照以下说明操作:

1:) Open your AndroidManifest.xmlfile.

1:) 打开你的AndroidManifest.xml文件。

2:) Go to the activity code which you want to make your main activity like below.

2 :) 转到要进行主要活动的活动代码,如下所示。

such as i want to make SplashScreen as main activity

比如我想让 SplashScreen 作为主要活动

<activity
    android:name=".SplashScreen"
    android:screenOrientation="sensorPortrait"
    android:label="City Retails">
</activity>

3:) Now copy the below code in between activity tags same as:

3 :) 现在将以下代码复制到活动标签之间,如下所示:

<activity
    android:name=".SplashScreen"
    android:screenOrientation="sensorPortrait"
    android:label="City Retails">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

and also check that newly added lines are not attached with other activity tags.

并检查新添加的行是否没有附加其他活动标签。

回答by user3154790

This is easy to fix.

这很容易修复。

  • Changes to the Launcher activity are also stored in the Debug configuration.
  • Go to Run > Debug Configurationsand edit the setting.
  • There is also a similar setting in Intellijunder Run > Edit Configurationsselect Run default Activityand it will no longer save the setting in this fashion.
  • 对 Launcher 活动的更改也存储在Debug 配置中
  • 转到Run > Debug Configurations并编辑设置。
  • Intellij 中的Run > Edit Configurationsselect Run default Activity下也有类似的设置,它将不再以这种方式保存设置。

回答by user3154790

It's simple. Do this, in your Manifestfile.

这很简单。在您的Manifest文件中执行此操作。

<activity
    android:name="Your app name"
    android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</activity>

回答by Null Pointer Exception

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

回答by user3206168

Just go to your AndroidManifest.xml file and add like below

只需转到您的 AndroidManifest.xml 文件并添加如下内容

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

then save and run your android project.

然后保存并运行您的 android 项目。

回答by Prabh deep

You add this you want to launch activity android:exported="true"in manifest file like

你添加这个你想android:exported="true"在清单文件中启动活动 ,比如

 <activity
      android:name=".activities.activity.MainActivity"
      android:windowSoftInputMode="adjustPan"
      android:exported="true"/>
  <activity

Open java file of this activity and right click then click on Run 'main Activity'

打开此活动的 java 文件并右键单击,然后单击运行“主活动”

OR

或者

Open java file of this activity and press Ctrl+Shift+F10.

打开此活动的 java 文件并按Ctrl+ Shift+ F10