سؤال

In my app, I would like to run the Splash screen once at first run only but the problem is that I already placed in the Manifest this line: android:noHistory="true" which works great if I press back button and exits the app but note that the app is still in the background running, and when I press the app icon it goes back again to the Splash screen then my Registration page. I wanted to be redirected to the Registration page directly when I reopen my application.

How do I do this? Thanks ahead for any suggestions.

هل كانت مفيدة؟

المحلول 4

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!

نصائح أخرى

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

}

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.

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top