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

Com*_*02x 6 android android-manifest android-intent android-activity back-stack

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

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

小智 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();
    }
}

}
Run Code Online (Sandbox Code Playgroud)


Com*_*02x 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);
Run Code Online (Sandbox Code Playgroud)

飞溅活动(恢复):

@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());
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的注册活动(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());
Run Code Online (Sandbox Code Playgroud)

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

@Override
public void onBackPressed() {
}
Run Code Online (Sandbox Code Playgroud)

非常感谢那些做出贡献的人!