How to launch activity only once when app is opened for first time?
What I've generally done is add a check for a specific shared preference in the Main Activity
: if that shared preference is missing then launch the single-run Activity, otherwise continue with the main activity . When you launch the single run Activity create the shared preference so it gets skipped next time.
EDIT : In my onResume
for the default Activity I do this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
if(!previouslyStarted) {
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
edit.commit();
showHelp();
}
Basically I load the default shared preferences and look for the previously_started
boolean preference. If it hasn't been set I set it and then launch the help file. I use this to automatically show the help the first time the app is installed.
Making an activity appear only once, when the app is started
Add this code to your onCreate method
SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
if(pref.getBoolean("activity_executed", false)){
Intent intent = new Intent(this, TutorialOne.class);
startActivity(intent);
finish();
} else {
Editor ed = pref.edit();
ed.putBoolean("activity_executed", true);
ed.commit();
}
SharedPreferences will be keep every time you execute the app unless you clean the data from Settings on your Android.
The first time is going to get the value from a boolean (activity_executed) saved on such preferences (ActivityPREF).
If it does not find any value it will return false, so we have to edit the preference and set the value to true.
The next execution will launch the activity TutorialOne
.
finish()
erases the current activity from the stack history, so no come back is possible using button back from TutorialOne.
About your manifest, you can set this actitiy with
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Every time the app is executed will launch this activity, but due to the true setted on the "activity_executed"
is going to start a new activity with startActivity
.
Launch an activity only once after Install
To complete @Pier Giorgio Misley answer you can put the "firstLaunch" check on your Main Activity or alternatively put it in another "splash" activity
For putting it in the main activity simply set the ui to some neutral color until you decide if you should finish the activity and launch the Web Activity or show the Main Activity logic
Alternatively, you can create a "splash" screen which can function as a bridge activity (which shows some logo or a nice background color) which check the varible and decide which activity to open Android splash
How to run an activity only once like Splash screen
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!
How to launch the app with a specific Activity only when the app is launched for the first time?
Above answers are also fine but if you need to do it without splash activity then you can use below method
Step 1:Create a new class and extends from Application and add below code (do not forget to change your activity name accordingly). The most important part of it is setting appropriate Intent Flags before starting any of the activities.
This GlobalActivity will be called before your launcher Activity
public class GlobalActivity extends Application {
@Override
public void onCreate() {
super.onCreate();
SharedPreferences userInfo = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = userInfo.edit();
boolean logedIn = userInfo.getBoolean("loggedIn", false);
if (logedIn)
{
Intent intent = new Intent(this,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
Intent intent = new Intent(this,LaunchOnceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
}
Step 2:
Your manifest file should be like below
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.appid" >
<application
android:name=".GlobalActivity"
android:allowBackup="true"
android:icon="@drawable/launch"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LaunchOnceActivity"
android:label="Launch Once" >
</activity>
</application>
</manifest>
And then change loggedIn varibale in any of the activity accordingly for future use.
opening activity only once NOT WORKING
This is how I do it, hope it helps:
First activity inside onCreate
if (pref.getString("someConstant",null) == null){
//This is my first time, no value in sharedpref for "someConstant"
//Feel free to do any logic here if needed
}else{
//not my first time
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
finish();
}
Now in your second activity, once it opens just set the shared preference inside onCreate
SharedPreferences.Editor ed = pref.edit();
ed.putString("someConstant","some random string, does not make difference").commit();
On first start your app won't have any value, therefore it will stay in the first activity because "someConstant" returns null
. Once you enter the second activity the value in shared preferences will be stored, so in any new app launch after that you will go straight to the second activity
How do I make an activity the first activity launched but only on the first run of the application
Don't have three separate LAUNCHER activities unless you want the user to be able to launch the app from the homescreen from three separate items (there are valid use-cases for this, but this isn't it).
Have one activity that handles launching the application. It checks the boolean in your SharePreferences, then starts the appropriate activity from there. Then just finish the Activity so the user won't re-open it when the user presses "Back".
public class LauncherActivity extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (firstTimeRunning()) {
// start register activity.
} else {
// start login activity.
}
finish();
}
}
In the Manifest just have this Activity as:
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
You don't need any IntentFilters for your register activity or login activity because these are never opened through implicit intents, nor do they have any special permissions (from what I can see).
How to make Activity to start only once when the app is started for the first time?
when the activity is started for the first time : save true value in
shared pref and everytime the app launches check the shared pref if
true go to next activity else show first activity (ur registration
page)
1.Declare variables
SharedPreferences pref;
SharedPreferences.Editor editor;
2.in onCrete method
pref = getSharedPreferences("testapp", MODE_PRIVATE);
editor = pref.edit();
3.When user successfully registers (click on register button)
editor.putString("register","true");
editor.commit();
Then every time u can check by :
String getStatus=pref.getString("register", "nil");
if(getStatus.equals("true"))
redirect to next activity
else
show registration page again
Related Topics
When How to First Measure a View
Read All Contacts' Phone Numbers in Android
How to Write Files to Assets Folder or Raw Folder in Android
Replace Getmap with Getmapasync
How to Override the 'Home' Button in My Application
Simple Parse JSON from Url on Android and Display in Listview
Going to Home Screen Programmatically
Ideal Way to Set Global Uncaught Exception Handler in Android
Recyclerview Header and Footer
How to Set Onclicklistener on a Radiobutton in Android
Firebase Analytics Custom Events Params
Android:Loading an Image from the Web with Asynctask
Programmatically Select Item Listview in Android
How to Get the First Name and Last Name from Android Contacts
How to Implement Recyclerview with Cardview Rows in a Fragment with Tablayout
Force Application to Restart on First Activity
Android, Canvas: How to Clear (Delete Contents Of) a Canvas (= Bitmaps), Living in a Surfaceview
Android Locationclient Class Is Deprecated But Used in Documentation