闪屏显示不止一次
Splash screen showing more than once
启动画面应该只在应用程序安装时出现一次。
AndroidManifest
<activity android:name=".Splash"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Splash.java
public class Splash extends AppCompatActivity {
private static boolean splashLoaded = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!splashLoaded) {
setContentView(R.layout.splash);
int secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
splashLoaded = true;
}
else {
Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}
}
但是我的即使在我输入 android:noHistory="true"
之后也不起作用
我还有什么要补充的吗?
您可以扩展 Application-class 以存储不同活动之外的变量。应用程序仅在应用程序启动时启动,每次打开特定 window 时都会启动 activity。有关扩展应用程序 class 的更多信息,请参阅此 link:https://www.google.be/url?sa=t&source=web&rct=j&url=http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/&ved=0ahUKEwi3qIaD-KzOAhVCWxoKHURMDgcQFggdMAE&usg=AFQjCNF0ZhP1KDNAUnJNY0YyvZvrm_Vn8g
public class Splash extends AppCompatActivity {
private static boolean splashLoaded = false;
SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
splashLoaded = prefs.getBoolean("Splash_Loaded", false);
if (!splashLoaded) {
setContentView(R.layout.splash);
int secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
splashLoaded = true;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("Splash_Loaded", true);
editor.apply();
} else {
Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}
}
使用此代码 -
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = getSharedPreferences("AnyName",MODE_PRIVATE);
Boolean loaded = sharedPreferences.getBoolean("loaded",false);
if (!loaded) {
setContentView(R.layout.splash);
SharedPreferences.Editor editor2 = sharedPreferences.edit();
editor2.putBoolean("loaded", true);
editor2.commit();
int secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
}
else {
Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}
启动画面应该只在应用程序安装时出现一次。
AndroidManifest
<activity android:name=".Splash"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Splash.java
public class Splash extends AppCompatActivity {
private static boolean splashLoaded = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!splashLoaded) {
setContentView(R.layout.splash);
int secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
splashLoaded = true;
}
else {
Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}
}
但是我的即使在我输入 android:noHistory="true"
之后也不起作用
我还有什么要补充的吗?
您可以扩展 Application-class 以存储不同活动之外的变量。应用程序仅在应用程序启动时启动,每次打开特定 window 时都会启动 activity。有关扩展应用程序 class 的更多信息,请参阅此 link:https://www.google.be/url?sa=t&source=web&rct=j&url=http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/&ved=0ahUKEwi3qIaD-KzOAhVCWxoKHURMDgcQFggdMAE&usg=AFQjCNF0ZhP1KDNAUnJNY0YyvZvrm_Vn8g
public class Splash extends AppCompatActivity {
private static boolean splashLoaded = false;
SharedPreferences prefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
splashLoaded = prefs.getBoolean("Splash_Loaded", false);
if (!splashLoaded) {
setContentView(R.layout.splash);
int secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
splashLoaded = true;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("Splash_Loaded", true);
editor.apply();
} else {
Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}
}
使用此代码 -
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = getSharedPreferences("AnyName",MODE_PRIVATE);
Boolean loaded = sharedPreferences.getBoolean("loaded",false);
if (!loaded) {
setContentView(R.layout.splash);
SharedPreferences.Editor editor2 = sharedPreferences.edit();
editor2.putBoolean("loaded", true);
editor2.commit();
int secondsDelayed = 5;
new Handler().postDelayed(new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
}, secondsDelayed * 500);
}
else {
Intent goToMainActivity = new Intent(Splash.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}