Android Studio Start Up App Tips And Hints - android

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
}

Related

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

How to save a result in android?

I'm a rookie in android programming. I have a small problem. When I click a ImageView, I make that ImageView invisible and set a Button to visible. My problem is that how do you save this? For eg, I click the ImageView, Button shows up and ImageView disappears. And I exit the app and enter back into that same activity and I want that Button to remain there. How do I go about doing that?
Thanks!
Use SharedPreferences. here is a good tutorial on how to use them. example
But basically you are good to go by adding this code to your Activity
private boolean isVisible;
#Override
public void onCreate(Bundle myBundle){
super.onCreate(myBundle);
isVisible = getPreferences(MODE_PRIVATE).getBoolean("visible", true);
.... your code
if (isVisible){
// show ImageView
} else {
//don't
}
}
}
public void onPause(){
if(isFinishing()){
getPreferences(MODE_PRIVATE)
.edit().
putBoolean("visible", isVisible).commit();
}
}
Use a shared preference to save the state, i.e. say in your case a boolean value to indicate whether imageview was visible or not when you exit the app.
When you launch the app, use this value and accordingly perform the action.
For usage of shared preference,
How to use SharedPreferences in Android to store, fetch and edit values
you can store the state in shared preference when you leave your app onPause() or on the click event and can get result back on onCreate() method from that preferences
To store data in shared preference(in OnPause() or on the click event):
SharedPreferences prefs = getSharedPreferences("yourPrefName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// save values
editor.putBoolean("isButtonVisible", true);
editor.commit();
To get data from sharedPrefs(in onCreate()):
SharedPreferences prefs = getSharedPreferences("yourPrefName", MODE_PRIVATE);
boolean btnstatus = prefs.getBoolean(Constants.IS_LOGIN, false);
if (btnstatus) {
//put the code to show button and hide imageview
}

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.

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

Categories

Resources