How to run an activity only once like Splash screen

拜拜、爱过 提交于 2019-11-28 09:27:52
Akshay Mathur

This is how I achieved it!Hope it helps!

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

}
Mohit

You can use Shared Preferences to save some boolean when you run your app at very first launch and then check that value on every launch if exits then directly start the Registration Activity.

Although, this approach of just saving a normal value has a loop hole where, suppose your app is installed on a user device and user just updated the app with new version without uninstalling the first one then in that case you also not gonna see the Splash as the old shared preferences will already be there with that old saved value.

So in that case you need to change the logic litle bit by saving the app version and check for app version on every launch in that way you will be able to produce real user experience.

Have a look at this : How to create a one-time welcome screen using Android preferences?

To elaborate on the mention of "shared preferences", I believe the following would work, if you inserted it in onCreate() of your main activity:

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

After the block executes, "firstTime" should indicate whether this is the first time the app has been run. "appInfo" is just a placeholder name for whatever you want the preferences file to be called, I believe.

So here's what I did, in my 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());
    }
}

In my 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());

And then disabled back button to prevent going back unless the user presses Home:

@Override
public void onBackPressed() {
}

Big thanks for those that contributed!

The simplest way it that You can use android:noHistory="true" in your manifest file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!