如何像Splash屏幕一样只运行一次活动

时间:2013-09-04 11:39:27

标签: android android-intent android-activity android-manifest back-stack

在我的应用中,我想在第一次运行时运行一次启动画面,但问题是我已经放在Manifest这一行:android:noHistory="true"如果我按下它会很有效按钮并退出应用程序,但请注意应用程序仍在后台运行,当我按下应用程序图标时,它会再次返回到启动画面,然后是我的注册页面。当我重新打开我的应用程序时,我想直接重定向到注册页面。

我该怎么做?提前感谢任何建议。

5 个答案:

答案 0 :(得分:11)

这就是我实现它的方式!希望它有所帮助!

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

public class check extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    SharedPreferences settings=getSharedPreferences("prefs",0);
    boolean firstRun=settings.getBoolean("firstRun",false);
    if(firstRun==false)//if running for first time
    //Splash will load for first time
    {
        SharedPreferences.Editor editor=settings.edit();
        editor.putBoolean("firstRun",true);
        editor.commit();
        Intent i=new Intent(check.this,Splash.class);
        startActivity(i);
        finish();
    }
    else
    {

        Intent a=new Intent(check.this,Main.class);
        startActivity(a);
        finish();
    }
}

}

答案 1 :(得分:3)

您可以使用共享首选项在首次启动时运行应用程序时保存一些布尔值,然后在每次启动时检查该值,如果退出则直接启动注册活动。

  

虽然这种只保存正常值的方法有一个循环漏洞,假设你的应用是   安装在用户设备上,用户刚刚使用new更新了应用程序   版本没有卸载第一个然后在那种情况下你也   不会看到Splash,因为旧的共享偏好已经存在   有那个旧的保存价值。

因此,在这种情况下,您需要通过保存应用版本来更改逻辑litle位,并在每次启动时检查应用版本,这样您就能够产生真实的用户体验。

看看这个:How to create a one-time welcome screen using Android preferences?

答案 2 :(得分:3)

详细说明“共享偏好”,如果您将其插入主要活动的onCreate()中,我相信以下内容可行:

    SharedPreferences settings = this.getSharedPreferences("appInfo", 0);
    boolean firstTime = settings.getBoolean("first_time", true);
    if (firstTime) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("first_time", false);
        editor.commit();
    }

块执行后,“firstTime”应指示这是否是第一次运行应用程序。 “appInfo”只是一个占位符名称,无论你想要调用首选项文件,我相信。

答案 3 :(得分:2)

所以这就是我在SplashActivity(onCreate)中所做的:

    SharedPreferences settings = getSharedPreferences("prefs", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("firstRun", true);
    editor.commit();

    Intent intent = new Intent(this, RegistrationActivity.class);
    startActivity(intent);

SplashActivity(的onResume):

@Override
public void onResume() {
    super.onResume();
    SharedPreferences settings = getSharedPreferences("prefs", 0);
    boolean firstRun = settings.getBoolean("firstRun", true);
    if (!firstRun) {
        Intent intent = new Intent(this, RegistrationActivity.class);
            startActivity(intent);
        Log.d("TAG1", "firstRun(false): " + Boolean.valueOf(firstRun).toString());
    } else {
        Log.d("TAG1", "firstRun(true): " + Boolean.valueOf(firstRun).toString());
    }
}

在我的RegistrationActivity(onCreate)中:

    SharedPreferences settings = getSharedPreferences("prefs", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("firstRun", false);
    editor.commit();

    boolean firstRun = settings.getBoolean("firstRun", true);
    Log.d("TAG1", "firstRun: " + Boolean.valueOf(firstRun).toString());

然后禁用后退按钮以防止返回,除非用户按下Home:

@Override
public void onBackPressed() {
}

非常感谢那些贡献的人!

答案 4 :(得分:0)

最简单的方式 您可以在清单文件中使用 android:noHistory =“true”