Showing the setup screen only on first launch in android - 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();
}
}
}

Related

Android Studio Start Up App Tips And Hints

I've looked around and I'm trying to see if Android Studio has any sort of function for when first term use of the app it shows some messages to help people.
I was thinking of using simple text boxes when on first create off the app/ first launch it would show the tips until they click on each object or item.
I know how to do the click and view just not sure the function to use for when they start the app?
You detect the first launch of your app for instance by loading (and defaulting) a boolean value from your app's preferences.
In your MainActivity
private boolean firstStart = true;
#Override
public void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = mContext.getSharedPreferences("APP", Context.MODE_PRIVATE);
firstStart = prefs.getBoolean("firstStart", true);
if (firstStart) {
// do your stuff on first start here
// like set "showWizard" or any other flag to true so
// your controls can behave in "firstStart" mode
// Don't forget to save, that it is now no longer the first start...
firstStart = false;
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstStart", false);
editor.apply();
}
}
Maybe try adding them into this:
#Override
public void onCreate() {
super.onCreate();
//put your lines in here
}

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

SharedPreferences for Initial User Setup

My application requires initial user setup, for the first time the app is opened by the user. He should be able to do some initial setup that will determine how the application is run. For this, I am using sharedPreferences. What Im doing is simply, setting the contentView depending on if the boolean is 'true' or false'. But the question i have is in regards to what happens after i setContentView().
//Variables
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//creating sharedpref file
PREFS_NAME = "MyPrefsFile";
shPref = getSharedPreferences(PREFS_NAME, 0);
shPref.edit().putBoolean("my_first_time", true).commit();
//checking if app's being launched for first time
if(shPref.getBoolean("my_first_time", true)) {
setContentView(R.layout.page_one);
//setting the shPref as false
shPref.edit().putBoolean("my_first_time", false).commit();
}
//checking if app has been launched before
else if(shPref.getBoolean("my_first_time", false) && shprefchecker == 1) {
setContentView(R.layout.home_page);
}
}
So what I want to do is wait for the user to finish setup before I can set the shPref as false. Im guessing that once the layout is changed in this line " setContentView(R.layout.page_one);", it will automatically set the shPref as false. I want the app to not do anything until the user has gone through the initial setup (first layout in the intial setup is page_one.xml).
I might sound confusing, but how would I do this? Please help. All answers are appreciated. Thanks!
I think it's pretty straight.
You can set the boolean to false on initial setup's Submit button click.
The set up page must have any button, right? So it will be ensured that user completes the setup first.
In your second activity, you will have to use:
shPref = getSharedPreferences(PREFS_NAME, MODE_WORLD_READABLE);
SharedPreferences.Editor pref_editor = shPref.edit();
pref_editor.putBoolean("first_time", false);
pref_editor.commit();
The problem I see in your code is that your code is that you put my_first_time to true always. Just dont define it, if you dont define a var in your shared pref it will return the default value you pass as the second parameter. I'll edit your code:
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//creating sharedpref file
PREFS_NAME = "MyPrefsFile";
shPref = getSharedPreferences(PREFS_NAME, 0);
//checking if app's being launched for first time
if(shPref.getBoolean("my_first_time", true)) { //my first time not defined, it will be true
setContentView(R.layout.page_one);
//setting the shPref as false
shPref.edit().putBoolean("my_first_time", false).commit(); //This line should go in your page_one activity when it ends, but when defined it will always go to home_page
}
//checking if app has been launched before
else if(shPref.getBoolean("my_first_time", false) && shprefchecker == 1) {
setContentView(R.layout.home_page);
}
}
Hope it helps :)
It is quite simple first You have to check bu getting shared preferences key value pair whether user has passed out through the setup process if not then You can start setup activity and if yes you can skip setup activity

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()..

AlertDialog restarted each time I return to MainActivity

I created a MainActivity in which the user has a few app options, displayed in a grid menu, which access subsequent specific activities. However, when the application starts, I use an AlertDialog for the user to enter login details, inflated just after the grid layout definition.
The problem is, each time I select an item in the grid menu (and, consequently, a new activity), the AlertDialog pops-up again. How can I avoid this?
Moreover, I have an uploading service which should start with the beginning of the MainActivity (or after the login, perhaps), but should not be restarted each time a new activity is called. I assume this problem is related to the previous one, although I have managed to temporarily solve it by using a startService button via an OptionsMenu. This is no permanent solution.
Thank you in advance.
EDIT: I tried to use getSharedPreferences as follows:
private SharedPreferences prefs;
private String prefName = "MyPref";
int hasLoggedIn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mm_gridmenu);
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
hasLoggedIn = prefs.getInt("hasLoggedIn", 0);
if (hasLoggedIn == 0) {
showDialog(SHOW_DIALOG);
prefs = getSharedPreferences(prefName , MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("hasLoggedIn", 1);
editor.commit();
}
However, this way the hasLoggedIn value is saved as 1 and the dialog never pops-up again. I tried setting the back button to fix that, but this seems to prevent the app from being minimized. Is there a way to add that action to the button? (Which I would duplicate on the Home button as well)
#Override
public void onBackPressed() {
prefs = getSharedPreferences(prefName , MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("hasLoggedIn", 0);
editor.commit();
Log.i("hasLoggedIn", hasLoggedIn + "");
return;
}
Moreover, I believe this action will affect subsequent activities (setting the alertDialog back on). Which should be a valid alternative to this?
Basically you need to keep track of your applications states, you have a few options to do this. One simple way would be to use a SharedPreferences to store a boolean variable called something like hasLoggedIn after the user logs in you set this value to true. Each time your main activity launches simply check the value of hasLoggedIn if its is set to false require the user log in again. If it is already true don't show the log in dialog
You can try this:
Add a boolean flag in your MainActivity:
private boolean dialogFlag = true;
in the onCreate/onResume method:
if(dialogFlag) {
createDialog();
dialogFlag = false;
}
If you want to pop up just once the app is installed, you can save this flag into a property file. And read it first whenever the app is getting started.

Categories

Resources