【发布时间】:2014-01-31 04:24:13
【问题描述】:
我正在为一个 android 应用程序实现一个启动画面。我只想在新启动应用程序时显示一次启动画面。完成一些工作后,我想继续使用该应用程序。如果用户然后按下后退按钮,我不希望它返回到启动画面,我只想退出应用程序。我怎样才能以最好的方式实现这一点?如何清除第一个活动的后台堆栈。
【问题讨论】:
标签: android splash-screen back-stack
我正在为一个 android 应用程序实现一个启动画面。我只想在新启动应用程序时显示一次启动画面。完成一些工作后,我想继续使用该应用程序。如果用户然后按下后退按钮,我不希望它返回到启动画面,我只想退出应用程序。我怎样才能以最好的方式实现这一点?如何清除第一个活动的后台堆栈。
【问题讨论】:
标签: android splash-screen back-stack
如果您只想在应用启动时首次显示启动画面, 那么您可以使用上述共享偏好解决方案。 但我想你想经历以下场景:
如果您遇到此问题,则需要完成启动画面
当您开始家庭活动时,最后您需要注销或完成应用程序家庭活动。
还可以在 android manifest 的启动屏幕活动选项卡中尝试 android:launchMode="singleTask"。
【讨论】:
在 android 中使用 shared preferences 并第一次存储值.. 从第二次开始检查值是否存在不显示闪屏
编辑偏好关注this
【讨论】:
当你在闪屏后进入第二个活动时,调用finish()
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (preferences.getBoolean("firstTime", true)) {
// Show splash screen
// Wait a few seconds.
} else {
// Nothing to do here. Go straight to the second activity.
}
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(
getSupportActivity()).edit();
editor.putBoolean("firstTime", false);
editor.commit();
startActivity(MainActivity.this, ...)
finish();
这样当用户回击时,堆栈中不会有任何活动。
【讨论】:
您好,试试这段代码,它会对您有所帮助,但它会在您打开应用程序时显示启动画面。
public class spash_scr extends Activity {
ImageView t;
//LoginButton b;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spash_scr);
// bm1=drawable.shineme;
t = (ImageView) findViewById(R.id.textView1);
t.setImageResource(R.drawable.shineme);
RotateAnimation r = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// r.setStartOffset(1000);
r.setDuration(2000);
r.setFillAfter(true);
t.startAnimation(r);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(spash_scr.this, MainActivity.class);
startActivity(i);
finish();
}
}, 3000);
}
【讨论】:
您并不清楚确切的行为,所以这里有一些选项:
A:您希望它在每次重新启动任务(应用程序)时显示启动活动,例如在手机重新启动后、用户手动关闭任务或 Android 因内存原因放弃它时。 (这通常用于品牌或许可徽标。)在这些情况下,从主活动的 onCreate() 启动启动画面,然后 Finish() 启动启动画面以允许用户返回主视图。这样导航不会将启动活动带回来,因为它不再位于导航堆栈中。
B:您希望它在安装后首次启动应用程序时显示启动画面,但不再显示。 (通常用于“欢迎”或“开始”帮助视图。)使用 SharedPreference 设置,如此处其他答案或Using Shared Preferences 文档中所述。在它应该显示启动画面的情况下,我仍然建议选项 A 是在首次启动后首次关闭后不再显示启动画面的最简单方法。
C:更未知的复杂导航?了解Tasks and Back Stack,您可以让它为所欲为。
【讨论】:
打开新Activitythis.finish ();后你应该这样做
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish ();
【讨论】:
为此,您应该使用 SharedPreferences ,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("first_time", false))
{
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("first_time", true);
editor.commit();
Intent i = new Intent(splash.this, otherSplash.class);
this.startActivity(i);
this.finish();
}
else
// Not firsttime Direct it as you wish
}
【讨论】: