How to start activity only once, like Splash screen

In my application, I would like to launch the Splash screen after the first launch, but the problem is that I already put this line in Manifest : android:noHistory="true" , which works fine if I press the "Back" button and exit the application but note that the application is still running in the background, and when I click on the application icon, it returns to the Splash screen again and then to my registration page. I wanted to be redirected to the registration page directly when I open the application again.

How should I do it? Thanks for any suggestions.

+4
source share
5 answers

So here is 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 turn off the back button to prevent a return if the user does not click Home:

 @Override public void onBackPressed() { } 

Many thanks to those who contributed!

+1
source

This is how I did it! Hope this 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(); } } } 
+9
source

To elaborate on the mention of “general preferences”, I believe the following will work if you added it to 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 execution of the block, "firstTime" should indicate whether this was the first time the application was launched. I think that "appInfo" is just a placeholder name for what you want the preferences file to be called.

+2
source

You can use General Preferences to save some logical values ​​when the application starts at the first start, and then check this value every time you start, if you exit, and then start the registration process.

Although this approach only has a hole in the loop to maintain normal value, where, suppose your application is installed on a user device, and the user simply updated the application using new versions without removing the first, then in this case you will not see Splash either, since the old common ones the settings will already be there with this old saved value.

Thus, in this case, you need to change the bit of the litle logic by saving the version of the application and checking the version of the application at each launch so that you can create a real user interface.

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

+1
source

The easiest way: You can use android: noHistory = "true" in the manifest file.

+1
source

Source: https://habr.com/ru/post/1500508/


All Articles