![](/img/trans.png)
[英]How can I launch the splash screen only when the app is killed or started for the first time ?
[英]How can i create only one time use splash screen for my android app
我想創建一個啟動畫面只出現一次的應用程序。 就像我剛剛從 Playstore 下載了應用程序,當我按下應用程序圖標時。 僅第一次顯示初始屏幕。 對於當時的 rest。 它不會像 go 在第二次活動中直接打開。 我怎樣才能做到這一點。 謝謝你。
您可以通過創建沒有 UI 和 SharedPreferences 的啟動器活動來實現此目的:
public class LauncherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("App", Context.MODE_PRIVATE);
// Get value from shared preferences,
// if null (app is run first time) the default value (second argument) is returned
boolean isFIrstRun = preferences.getBoolean("isFirstRun", true);
if (isFIrstRun) {
// set isFirstRun to false
preferences.edit().putBoolean("isFirstRun", false).apply();
// launch splash screen activity
} else {
// Launch other activity
}
}
}
並確保 LauncherActivity 在應用清單中設置為啟動器活動。xml:
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.