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();
}
}
Related
I need to create a splash screen that should displays only at the first time when an application is installed and launched and from the second time the when the app is launched it should shows the main activity?How can i achieve it ?
Okey looking at your problem you can do following..
First of all declare object of SharedPreference and on String which will we use later.
SharedPreferences loginPreference;
String MY_PREF = "my_pref";
Now in onCreate of your SplashActivity, do something like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// initialize SharePreference
loginPreference = getSharedPreferences(MY_PREF, Context.MODE_PRIVATE);
// this condition will do the trick.
if(loginPreference.getString("tag", "notok").equals("notok")){
// add tag in SharedPreference here..
Editor edit = loginPreference.edit();
edit.putString("tag", "ok");
edit.commit();
// your logic of splash will go here.
setContentView(R.layout.splash);
}else if(loginPreference.getString("tag", null).equals("ok")){
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}
Happy Coding..
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
}
i want to display a layout that takes user information and save them.
Then, when the user open the application for second time it shows different layout.
How i can do that??
and Please if you can help by just mentioning the right keywords so i can also search by myself I will be great-full
Use this in your onCreate() method:
#Override
protected void onCreate(Bundle bundle) {
boolean firstStart = getSharedPreferences("SomeName", MODE_PRIVATE).getBoolean("firstStart", true);
if(!firstStart){
Intent intent = new Intent(this, SomeOtherActivity.class);
startActivity(intent);
}
}
Make sure you write false to the shared prefferences after your first start:
SharedPreferences prefs = getSharedPreferences("SomeName", MODE_PRIVATE);
Editor edit = prefs.edit();
edit.putBoolean("firtStart", false);
edit.apply();
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.
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();
}
}
}