如何使活动成为第一个启动的活动,但仅在应用程序的首次运行时 [英] How do I make an activity the first activity launched but only on the first run of the application

查看:53
本文介绍了如何使活动成为第一个启动的活动,但仅在应用程序的首次运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的应用程序具有一个活动(注册)是首次安装应用程序时启动的第一个活动,但是在首次运行后,它将具有不同的活动(LogInActivity)是启动的第一个活动...将代码放入其中会使Register活动仅运行一次,但我不知道如何更改清单以获取所需的结果.我曾尝试将MAIN动作移至Register活动,但这导致它在我每次运行我的应用程序时启动.当MAIN操作位于LogInActivity中时,Register根本不会运行.我还尝试在清单中添加与我执行MAIN动作相反的活动下的DEFAULT动作,但这也没有用,并且在进一步研究DEFAULT的内容时,我认为这不是我所需要的.那么我还需要添加其他一些意图吗?

I need my app to have one activity(Register) be the first activity launched when an app is first installed but then after the initial run it will have a different activity(LogInActivity) be the first activity launched... I have put code in that makes the Register activity only run once but I can't figure out how to alter my manifest to get the desired results. I have tried moving the MAIN action to the Register activity but that causes it to be launched everytime I run my app. When the MAIN action is in the LogInActivity then Register doesn't run at all. I also tried to add a DEFAULT action in my manifest under the opposite activity of where I had the MAIN action but that didn't work either and upon further research of what DEFAULT does I don't believe this is what I need. So is there some other intent I need to add to my XML?

清单:

<application
    android:allowBackup="true"
    android:icon="@drawable/skey"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

 ///////////FIND INFO ACTIVITY
    <activity
        android:name=".FindInfoActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.LogInActivity" />

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

  //////////LOG IN ACTIVITY
    <activity
        android:name=".LogInActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.LogInActivity" />
            <action android:name="android.intent.action.MAIN" />

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

  //////////REGISTER ACTIVITY
    <activity
        android:name=".Register"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.LogInActivity" />

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

</manifest>

推荐答案

除非您希望用户能够从主屏幕上通过三个单独的项目启动应用程序,否则没有三个单独的LAUNCHER活动(有效使用-这种情况,不是吗.

Don't have three separate LAUNCHER activities unless you want the user to be able to launch the app from the homescreen from three separate items (there are valid use-cases for this, but this isn't it).

具有一个处理启动应用程序的活动.它检查您的SharePreferences中的布尔值,然后从那里开始适当的活动.然后只需完成活动,这样用户在按下返回"时就不会重新打开它.

Have one activity that handles launching the application. It checks the boolean in your SharePreferences, then starts the appropriate activity from there. Then just finish the Activity so the user won't re-open it when the user presses "Back".

public class LauncherActivity extends Activity {

   @Override
   public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     if (firstTimeRunning()) {
         // start register activity.
     } else {
         // start login activity.
     }
     finish();
   }
}

在清单中,只需将此活动设置为:

In the Manifest just have this Activity as:

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

您不需要任何IntentFilter用于您的注册活动或登录活动,因为它们永远不会通过隐式意图打开,也没有任何特殊权限(据我所知).

You don't need any IntentFilters for your register activity or login activity because these are never opened through implicit intents, nor do they have any special permissions (from what I can see).

这篇关于如何使活动成为第一个启动的活动,但仅在应用程序的首次运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆