如何仅在应用程序“新鲜”启动时显示启动屏幕?

发布于 2024-12-08 14:35:18 字数 1741 浏览 0 评论 0原文

我想在应用程序生命周期中仅显示一次启动屏幕。这是我的代码:

SplashScreenActivity.java:

final int welcomeScreenDisplay = 3000;

Thread welcomeThread = new Thread() {

    int wait = 0;

    @Override
    public void run() {
        try {
            super.run();

            while (wait < welcomeScreenDisplay) {
                sleep(1000);
                wait += 1000;
            }
        } catch (Exception e) {
            System.out.println("EXc=" + e);
        } finally {

            // Start other Activity
            startActivity(new Intent(SplashScreenActivity.this,
                    MainActiviey.class));
            finish();
        }
    }
};
welcomeThread.start();

清单:

    <activity android:name=".SplashScreenActivity" android:label="test"
    android:noHistory="true"
        android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActiviey" android:label="test"
         android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

问题是如果我按硬件主页按钮隐藏应用程序并在应用程序列表中再次打开应用程序。它将再次显示启动屏幕(而不是显示 MainActivity)。 是否可以仅在应用程序“新鲜”启动时显示启动屏幕(不在 onresume() 处显示)?谢谢!

I want to show the splash screen only once during the Application life cycle. Here is my code:

SplashScreenActivity.java:

final int welcomeScreenDisplay = 3000;

Thread welcomeThread = new Thread() {

    int wait = 0;

    @Override
    public void run() {
        try {
            super.run();

            while (wait < welcomeScreenDisplay) {
                sleep(1000);
                wait += 1000;
            }
        } catch (Exception e) {
            System.out.println("EXc=" + e);
        } finally {

            // Start other Activity
            startActivity(new Intent(SplashScreenActivity.this,
                    MainActiviey.class));
            finish();
        }
    }
};
welcomeThread.start();

Manifest:

    <activity android:name=".SplashScreenActivity" android:label="test"
    android:noHistory="true"
        android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActiviey" android:label="test"
         android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

The problem is if I press the hardware HOME botton to hide the app and open the App again at the application list. It will show the splash screen again (instead of showing the MainActivity).
Is it possible to show splash screen only when the app starts "fresh" (not show at onresume() )? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-12-15 14:35:20

您不能对两个活动有此意图:

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

此外,您可能希望从历史记录堆栈中隐藏启动屏幕,以防您决定按照 Ash 建议在其中启动另一个活动。

您可以在活动标签上使用此标志:

android:noHistory="true"  

You can't have this intent for two activities:

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

Also you might want to hide the splash screen from the history stack in case you decided launch another activity inside it as per Ash suggestion.

You can use this flag on your activity tag:

android:noHistory="true"  
触ぅ动初心 2024-12-15 14:35:20

是的,这是可能的。使用 SharedPreferences 来存储一个标志,这表明您的启动画面已经已显示。在启动屏幕的 onCreate() 方法中检查它,如果存在,则启动下一个活动。

Yes, it's possible. Use SharedPreferences to store a flag, that would indicate that your splash has already been shown. Check it in onCreate() method of your splash screen and if it is present, launch the next activity.

孤芳又自赏 2024-12-15 14:35:20
public class SplashActivity extends AppCompatActivity {

Handler handler;
private final int SPLASH_DISPLAY_LENGTH = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    SplashStart();

}

private void SplashStart() {
    handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
}


@Override
protected void onResume() {
    super.onResume();

    }
}
public class SplashActivity extends AppCompatActivity {

Handler handler;
private final int SPLASH_DISPLAY_LENGTH = 2000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    SplashStart();

}

private void SplashStart() {
    handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
}


@Override
protected void onResume() {
    super.onResume();

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文