How to trigger a tutorial on first app launch(Android) - java

I need to start an activity(tutorial) on my first app launch. I figured i could write this in my main but then i realized once the app is closed and start again it would just start the tutorial again.
Boolean first = true;
if(first){
Intent i ......
first = false;
}
so i thought about creating a database or writing a value in a file and saving the Boolean value in that. Is there a easier way for this? thanks in advance

You can use preference for this. You can implement following codes on your onCreate method
SharedPreferences prefs = PreferenceManager.getDefaultPreferences(getApplicationContext());
boolean first = prefs.getBoolean("key_first_launch", true);
if (first)
// show your tutorial
else
// dont show your tutorial
When first tutorial complete, change preference value
SharedPreferences prefs = PreferenceManager.getDefaultPreferences(getApplicationContext());
prefs.edit().putBoolean("key_first_launch", false).commit();

You need to save that information, for instance with SharedPreferences, so that it is persistent and read its value when the application is started.
See:
http://developer.android.com/reference/android/content/SharedPreferences.html
http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html#preferences

Related

How to pass the same data with different value with putExtra()

I am working with a service class that needs to know if the application is running in background or not. To do so, I created a boolean variable called activityVisible in my service. I need to update it constantly to know wether the app is in background or active.
I do this by sending true on the onResume() method in my activity like this:
intent.putExtra("Activity Visible", true)
and false on the onPause() method like this:
intent.putExtra("Activity Visible", false)
However, I print the extra in my service and it is always getting true. What I think it is happening is that once you put an extra, you cannot update its value, and that may be the reason it is not working.
Any way I can do the same in a different way? Or a solution to the way I am implementing this?
You can use SharedPreference to store the current state of the app.
to reference to the SharedPreference
SharedPreferences sp = getSharedPreferences("prefs", Context.MODE_PRIVATE);
to set a boolean in the shared preference, you need to use its editor, so apply this code in onPause, and onResume, while changing the "isVisible" value based on your need
boolean isVisible = true;
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("activity_visible", isVisible);
editor.apply(); // use apply to confirm the edit
and in order to get the value stored, you can simply refer to it using
boolean valueStored = sp.getBoolean("activity_visible", false /* Default value if not existed */);
Log.d("Activity State: ", "Boolean: " + valueStored);
and do whatever you want with that valueStored.

Store variable in shared preferences and changes their value on different activities

I'm working on a practise 7 days exercise app. I set a progree bar on the main activity and want to change progress after days. Means if i complete exercise of day 1 ,progress bar set 15% . and when i complete day2 , progress set 30%. I can do it without shared preference , it working correctlt but when after day1 complete i closed the app it again set progress from 0 . So i want to use shared preferences for this reason . Kindly someone guide me regarding this issue;
For Set Value to Shared Prefrence
SharedPreferences.Editor editor = getSharedPreferences("ProgressBarData",
MODE_PRIVATE).edit();
editor.putInt("progress", 15);
editor.apply();
for get value From Shared Prefrence
SharedPreferences prefs = getSharedPreferences(ProgressBarData,
MODE_PRIVATE);
int progress = prefs.getInt("progress", 0);
1st ur know ur mistake
u can't store ur data in local variable because at the end of the activity it destroyed every things and when u come back to android activity it will be started all thing once again and every thing is restarted
[https://developer.android.com/guide/components/activities/activity-lifecycle
read this u have better understanding
Now ur solution
if u want to store the data and process every day better to use local storage like
Sqlite ,room or shared preference.
Step to do the task
There is three step to store, get and remove data into share preference
For storing , getting deleting data
//storing
SharedPreferences.Editor editor = context.getSharedPreferences(name,Context.MODE_PRIVATE).edit();
editor.putString(key, data);
editor.apply();
//getting
SharedPreferences getSharedPrefrence = context.getSharedPreferences(name, Context.MODE_PRIVATE);
int data = getSharedPrefrence.getInt(key, IntegerValuesAndStringValues.REGISTER_BEFORE_LOGIN);
return data;
BasicFunctions.removeSharedPrefrences(getContext(),"Name of the preference");

Entering app exactly as it was left

I know there must be douzens of answers to this question out there, but either i cant't find them or I don't understand them.
The Question:
How do I get my app to exactly start as it was left?
F.e. dynamicly added checkBoxes shouldn't dissapear!
There is no "out of the box" way of doing it. You could save the current state of your Activity in some way (More on persistence)
Then you need to be able to rebuild the desired state of the persisted state in your Activity lifecycle
You could save and load with the shared preferences for example:
public void saveState(YourState state) {
SharedPreferences sharedPreferences = app.getSharedPreferences(R.string.preference_file_key, Context.MODE_PRIVATE)
sharedPreferences.edit()
.putString("CustomAtt", state.getCustomAtt())
}
public YourState loadState() {
SharedPreferences sharedPreferences = app.getSharedPreferences(R.string.preference_file_key, Context.MODE_PRIVATE)
String customAtt = sharedPreferences.getString("CustomAtt", "DefaultValue")
return new YoutState(customAtt)
}
And use it like this
#Override
protected void onCreate(Bundle savedInstanceState) {
YourState state = loadState();
// Rebuild your activity based on state
someView.setText(state.getCustomAtt())
}
You can store such values in SharedPreferences.
https://developer.android.com/training/basics/data-storage/shared-preferences.html
It is using key-value approach for saving. So you can save some values and read it from SharedPreferences whenever you want to.
This is the best approach for small data, that can be used on the app launch. So you can quit your app and the data is still present - so can be read on the next app launch.
Or save the condition of your program to a text file, so that the program can "translate" it back into conditions before it stops, or what I do not recommend, it saves every object created with ObjectOutputStream.

How to show different page if user is first time user (Android)

I have a blank-ish Android Project and what I want to do is take the user to a different "page/screen" if it is their first time only.
I know the logic for this but since I'm new to Android, I'm unsure of how to code this.
Below are the steps I believe I need to take in order to accomplish this:
App loads. If Local storage contains setting "FirstTimeUser", then it is not their first time using the app. Show the MainActivity page. If FirstTimeUser setting does not exist, it is their first time using the app (or they have uninstalled and reinstalled it), so instead, show WelcomeActivity page.
After viewing Welcome activity page, create FirstTimeUser setting and set to False.
But how do I code this for an Android app?
Use shared preferences as shown:
//declare as global
SharedPreferences prefs = null;
//and in your onCreate method:
prefs = getSharedPreferences("packageNameHere", MODE_PRIVATE);
if (prefs.getBoolean("firstrun", true)) {
//do stuff here if first run
//make sure to flag the boolean as false
prefs.edit().putBoolean("firstrun", false).commit();
}
else{
//if not first run, do something else
}
There is something called "SharedPreferences" that i believe your looking for.
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html

Doing an action exactly once

I`m trying to add tracking events to my android app using Mixpanel (it simply tracks what actions the user is doing in an application), and I want to add an event called "Application Started".
The question is, where should I track the event of the app starting without having it repeated. Is there a method or a function call in the life cycle that executes when the app first starts and only once ?
Do it in onCreate() of your application class.. it will be done only once when app gets started...
No, application class is different from activities.. For an application, there can be only on application class.. And that you declare in the manifest file.. Typically like this:
public class MyApplication extends Application {
#override
onCreate()
{
// Do your task here..
}
}
You can save it in as a preference value:
SharedPreferences setting = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstStart = setting.getBoolean("firstStart",true);
if(firstStart == true) {
//do work first time
SharedPreferences.Editor edit = setting.edit();
edit.putBoolean("firstStart", false);
edit.commit();
}

Categories