How do I make an activity the first activity launc

2020-08-04 10:51发布

问题:

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?

Manifest:

<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>

回答1:

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).

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>

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).



回答2:

Use SharedPreference. When you open the app the first time do this:

SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
shared.edit().putBoolean("first_time",true).apply();

in your onCreate() of first Activity do this:

SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
if(shared.getBoolean("first_time",false)){
startActivity(new Intent(FirstActivity.this, NewFirstActivity.class));
finish();
}

Is the easiest way to "storage" data and states. SharedPreference also works with Strings, Int values, etc (if you don't want boolean value)



回答3:

In your Manifest you should have only one launcher class, in the same class's onCreate method check for already logged in condition (SharedPreference). If yes, then using Intent launch HomeActivity otherwise launch LoginActivity if the user has registered and logged out. When user logs in, save account id / session details / user details in SharedPreference which you can validate in your launcher class for starting specific activity.



回答4:

As Mariano said, you can use SharedPreferences. If you don't want to open your settings at startup you can use a Dialog to let the user decide to go to your settings or do nothing.

By the way, I think that use SharedPreferences to store login information (username and password) is best practice than be asking these values every time you start an app.