How to run an activity only once like Splash screen - android

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.

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.

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.

Related

Showing a message only to new installations

I have an app developed on android studio, and when you start its showing a SplashScreen with the icon an a loading bar, and when its done, the app auto start a class called Slider and have 4 sliders for the new users to know how to use the app and then, the user click on a button and redirect to MainActivity, ok, so i want, if a users is old, to don't display the Slider class and redirect automatically to MainActivity, I'm saying, to display the slider only for new installations, if anyone can help me.. y tried so many hours an i don't get nothing works... Thanks to all!
Only for new installation.
Use SharedPreferences and store your first installation flag to run your Settings at first time.
Like,
private SharedPreferences prefs = null;
private void isFirstTime()
{
prefs = getSharedPreferences("appPref", MODE_PRIVATE);
if (prefs.getBoolean("firstInstall", true)) {
// Your setting activity start here
prefs.edit().putBoolean("firstInstall", false).commit();
}
}
Hi maybe i understand your problem, maybe one of more solution:
SharedPreferences prefs = context.getSharedPreferences("com.example.app", context.MODE_PRIVATE);
//check if the first run of app
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
//for example i run another class if is the first run
Intent intent = new Intent(context, MyClass.class);
context.startActivity(intent);
}
if Slider is the Launcher the in your Slider.class use Shared Preference,just add this lines as first lines in your public void init(Bundle bundle) method
SharedPreference pref;
#Override
public void init(Bundle bundle) {
pref =SharedPreference.getSharedPrefernce("APP",Context.MODE_PRIVATE);
if(pref.getBoolean("first",false)) // false for first time so it wont start Mainactivity without slider
{
SharedPreference.Editor editor= pref.edit();
editor.putBoolean(true);// from second time it will be false
editor.commit();
Intent in=new Intent(Slider.this,MainActivity.class);
startActivity(in);
}
//Existing Slider Code
}
FINALLY I SOLVED THIS!! AFTER COUPLE HOURS.. thanks to everyone.
Finally i put on MainActivity in onCreate this:
SharedPreferences prefs = null;
{
prefs = getSharedPreferences("com.sorte.app", MODE_PRIVATE);
if (prefs.getBoolean("firstInstall", true)) {
Intent i = new Intent(MainActivity.this, Slide.class);
startActivity(i);
prefs.edit().putBoolean("firstInstall", false).commit();
}
}

How to launch settings on the first run of the app

I have developed an Android app and it run effectively though I want launch the settings activity on the every first run (after installation) then for the rest of its life cycle, it should launch the main activity. How do I go about that?
Simply check the existing settings and launch the settings activity if they do not exist (files missing or shared preferences are not defined). Or, define a preference like FIRST_RUN with the default value true that you set to false on the first run.
See Android storage options on how to remember settings on Android.
Set it as the MainActivity and create a Shared Preference so it wont load every time the Application loads..
MainActivity
private final String KEY = "SHARED_KEY_FIRST_RUN";
public void onCreate() {
SharedPreferences pref = this.getSharedPrefereneces(....,Context.MODE_PRIVATE);
if(pref.getInteger(KEY,0) == 0) {
//Do first Time Loading.
}
else {
Intent intent = Intent(this,SecondActivity.class);
startActivity(intent);
}
}
dont forget to declare the SecondActivity in the Mainfest and save the Key with SharedPreferences.Editor
try this :
private boolean isFirstTime()
{
preferences = getPreferences(MODE_PRIVATE);
ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return !ranBefore;
}
So, if you want to check:
if (isFirstTime()){
//if first time do something
}

Call an activity only once in the app and show it again when the app restart after being killed

I'm developing an application in which I have to show an activity only once in the app lifecycle.
What I'm doing is on my MainActivity.java I'm calling an Activity 1, so after when I move in my app and whenever I come back to MainActivity.java my Activity 1 is called. I just want to show it once.
And again Activity 1 should be displayed when user kills the app and restarts it.
Here is what I'm doing in my MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(MainActivity.this,
Activity1.class));
}
I have tried using the following code but it only run once, when the app is installed for the first time.
private boolean isFirstTime() {
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return !ranBefore;
}
How can I modify the above code, so that my requirement is satisfied.
Any kind of help will be appreciated.
You should set ranBefore to false in onDestroy
#Override
public void onDestroy()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", false);
editor.commit();
}
Change
return !ranBefore
to
return ranBefore
It looks to me that you are always returning the same thing instead of the variable you initialize. Also, you could put the code to start the Activity directly in that method. Then you don't have to even worry about a return statement. It will just never run it again after you change the value in SharedPreferences
Edit
you can set your SharedPreferences value to false in onCreate(). This will work if you don't finish your Activity when you go to another and if its your main Activity then you probably don't ever want to finish it until you exit the app
I have solved this problem using SharedPreferences. What I have done is on Splash I entered some values in SP, and on the MainActivity I checked that, if the value matches show the activity, otherwise don't open the dialog. And on keyCodeBack(), I have cleared SP, this helps me in meeting my requirement.
Use shared preferences..
and to kill the activity, use class.finish() at your onClick()..

Showing the setup screen only on first launch in android

I am making an android application but i can't figure out how i can make the setup screen show up only the first time.
This is how the application is going to work:
User launches the application after installation and is being shown the welcome/setup screen. And once the user is done with the setup, the setup screens will never appear again unless the user reinstalls the application.
How can i make this happen???
Please help and thanks SO much in advance!
Use SharedPreferences to test whether its the first start or not.
Note: The below code was not tested.
In your onCreate (or whereever you want to do things depending on first start or not), add
// here goes standard code
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
if(pref.getBoolean("firststart", true)){
// update sharedpreference - another start wont be the first
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("firststart", false);
editor.commit(); // apply changes
// first start, show your dialog | first-run code goes here
}
// here goes standard code
Make one helper activity. This will be your launcher activity.It will not contain any layouts, It will just check for first fresh run of an app. If It will first run, then setup activity will be started otherwise MainActivity will be start.
public class HelperActivity extends Activity {
SharedPreferences prefs = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
//strat DataActivity beacuase its your app first run
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
startActivity(new Intent(HelperActivity.ths , SetupActivity.class));
finish();
}
else {
startActivity(new Intent(HelperActivity.ths , MainActivity.class));
finish();
}
}
}

Start activity only once

I want my app to start a activity only the first time the app launches.
Any one got any idea?
I found this but it only shows a black screen.
public class WhatsNew extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
protected void onCreate(Bundle state){
super.onCreate(state);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean dialogShown = settings.getBoolean("dialogShown", false);
if (!dialogShown) {
// AlertDialog code here
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("dialogShown", true);
editor.commit();
}
}
}
When the app launches, set a flag in the activity preferences that the activity has already run. Default your setting to false, and then only start that activity if the flag isn't set. Note that if the user cleans your application data, or uninstalls it and later installs it again the activity would show again.
I think that you talk about activities like "log" page that you would launch only once in your application's whole life.
For this, you can use sharedPreferences:
SharedPreferences prefs;
SharedPreferences.Editor editor;
and below your startActivity(intent), you add:
prefs = getSharedPreferences("nbRepet",Context.MODE_PRIVATE);
editor = prefs.edit();
editor.putInt("nbRepet", 1);
editor.commit();
Now your variable nbRepet have 1 as value.
After that, you can add these lines ABOVE your startActivity(intent) to verify that your activity was never launched before:
//here you recover nbRepet's value..
preferences = MainActivity.this.getSharedPreferences("nbRepet", MODE_PRIVATE);
int value = preferences.getInt("nbRepet", 0);
//.. and you verify if your activity is launched before.
if(value<1)
{
startActivity(intent);
}
You will need an Activity which checks a persisted boolean. ie,
onCreate(Bundle bundle)
{
boolean firstRun = // persistance method here
Intent toForward;
if(firstRun)
// Create an intent to start you "only once" activity
// persist "firstRun" as false;
else
// Create an intent for your usual startup
startActivity(toForward);
finish();
}
this will do the job for you
package com.example.startup;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main); //we don't need this line
SharedPreferences settings=getSharedPreferences("prefs",0);
boolean firstRun=settings.getBoolean("firstRun",false);
if(firstRun==false)//if running for first time
{
SharedPreferences.Editor editor=settings.edit();
editor.putBoolean("firstRun",true);
editor.commit();
Intent i=new Intent(MainActivity.this,Second.class);//Activity to be launched For the First time
startActivity(i);
finish();
}
else
{
Intent a=new Intent(MainActivity.this,Main.class);//Default Activity
startActivity(a);
finish();
}
}
}
Depends on what "first time" means. Easiest way is to put some flag into the SharedPreferences... but that can stay around for a while. ^^
If you mean that onCreate should be invoked only once when "start only once when app is launched", you could set the launchMode of the acitivty to singleInstance or singleTask in the manifest.
See my answer in this post, regarding this same issue.
I recommend checking the SharedPreference value for whether the app has previously been ran, inside of the onResume().

Categories

Resources