自动更改启动另一个activity的activity

Automatically change the activity of starting another activity

我有两个活动,一个activity是一个"AppIntro",另一个是"Home page"。我想显示一次"AppIntro",然后应用程序启动时不显示直接进入首页。

有一种方法可以从 activity.java.

中操纵 intent-filters
    <activity
        android:name=".DefaultIntro"
        android:label="@string/title_activity_default_intro"
        android:theme="@style/FullscreenTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.Launcher">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

您可以在 AppIntroActivity 中将布尔标志保存为 false。

当您第一次打开此 Activity 时,标志为 false,activity 将加载。

当您从这个 activity 移动到另一个时,请将标志设置为 true。

在 AppIntroActivity 的后续 return 中,如果标志为真,则立即移至第二个 activity。

private SharedPreferences sharedPreferences;

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
final boolean hasOpenedBefore = sharedPreferences.getBoolean("HASOPEN", false);

if(hasOpenedBefore){
// move immediately to the next Activity
// secondly, remove it from the top activity stack
}

我认为没有任何方法可以操纵清单文件中定义的意图过滤器。你可以这样做

  1. 当您打开 DefaultIntro activity 时,在 SharedPreference 中保存一个布尔值并将其设置为 false
  2. 下次检查这个值,如果是 false 直接跳到下一个 activity 而不设置它的布局。

DefaultIntro.javaonCreate

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean showIntro = prefs.getBoolean("showIntro", true);
if(showIntro)
{
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("showIntro", false);
    editor.commit();
}
else
{
    Intent intent = new Intent(this, MainActivity.java);
    startActivity(intent);
    finish();
}