I made an android app and I have added an agreement activity in it.But every time I start the app it starts the agreement page.I want that After the application is installed,it should not show the agreement page every time but a specific activity should be launched.How to do that?
You might want to use SharedPreferences
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean wasShown = prefs.getBoolean("agr", false);
if(!wasShown) {
showAgr();
prefs.edit().putBoolean("agr", true).commit();
}
Use SharedPreferences to identify whether the app is running for the first time.
Look at this question
Related
The scenario is if the user downloads the app for first time, I ask them basic questions (say Activity A) and request them to sign up (Activity B).
This is a one time process only after installing the app. After that whenever they open the app, I am planning to take them straight away in to the app (Activity C).
How should I do this? I am a newbie to Android programming. But I am not able to think about this scenario. I don't want to use database.
You need a persistent storage mechanism to save the state of the user (logged in or not). There are various ways you can do this. The easiest is SharedPreference which will store the user state locally. You can also store this information in your remote server and validate user each time she opens the app although this might be going a bit overboard in most cases.
Try using SharedPreferences. In ActivityA in on create check if SharedPreferences contain a certain value which decides if the user is signed up. If it not set or it does not have the required value, redirect the user to ActivityB or else ActivityC
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if(pref.contains("MY_KEY") && pref.getBoolean("MY_KEY", false)){ //first stratup or user has not signed in yet
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
} else { //already signed up
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
setContentView(R.layout.activity_main);
}
Dont forget to save/insert value inside SharedPreferences after the user sign up.
Code:
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
if(sign_up_success){
editor.putBoolean("MY_KEY", true);
editor.commit();
}
The basic flow should be as follows Splash Screen -> OnBoarding Screen(Signup) -> MainActivity On the Splash Screen you need to check for a prefs value lets call it isSignedUp which is false if the user has not signed up. Its pretty straightforward from here. Every time you launch your app you need to check if the preference if isSignedUp is true or false and based on the value show the screen accordingly. So if the value is true that means the user has signed up and show him the main screen else show him the sing up screen.
Use Android SharedPreferences, it can be used to store data which can be used to keep the answers in cache.
I am developing an android device anti-theft application in which the user's phone would automatically send a sms to the recovery mobile numbers with new numbers' details on boot. I used SmsManager with BroadCastReceiver and everything was good till that. But I want the UI part to be visible when I install the app to get the recovery numbers from the users only for the first time and then hide the app and only send sms'on boot later(after first time,work only on background). Is there a way to achieve this?
Use PackageManager to disable the Activity that has the main launch intent filter after setup is complete.
So many possibilities which you can try out are available
Use shared preferences in application. This checks whether our application installed is populated with the values that you want to cross check during boot up time
Use SQLITE DB to store values which can be encrypted. Check for the business logic and process accordingly.
Update data in your server and every time app is up , cross check with server data and run your logic.
File system (creating file and writing/reading data) This is the last and not advisable option
If you want references , how to make all these pls refer the following links
Android Shared Preference ExampleAndroid Sqlite exampleAndroid file System example
I guess you need to launch the Activity exactly once after it has been installed. In that case, when that specific activity is launched you can create a SharedPreference and save a value in there! And next time you launch, check if that value exists, if it doesn't launch the activity, else do the other option. Here is how I did it.
In the Activity to be launched once:
SharedPreferences pref = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = pref.edit();
ed.putString("HASH", hash_received);
ed.putString("MOB",mob_no);
ed.apply();
And then in Activity before that, I implemented:
SharedPreferences pref = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
String abc = pref.getString("HASH","");
if(abc.equals(Test))
{
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
else
{
startActivity(new Intent(Splash.this, FeedActivity.class));
finish();
}
Hope it helps! Happy coding!
I'm developing an Android application and I want to test the first launch, i.e., install, of the app.
I'm currently using an ApplicationTestCase and a hardware device. However, I always have to delete the app from my device before re-running the test. Additionally, the onCreate() method of the application class is called before the first test-method. Hence, giving me a hard time to verify actions of the first call.
Is there an approach that avoids these issues?
Thanks for your help in advance!
You could have the onCreate() method test for a specific property in the app's preferences, to determine if this is the first call. Something like this:
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("first_start", true)) {
//first start
settings.edit().putBoolean("first_start", false).commit();
}
How can I set an Activity that will be the Launch Activity just one time ( when the user sign up )?
e.g.
I have a Settings Activity and a NewsFeed Activity.
The first time I want to lanch the app with the Settings Activity and, after the user fill the options, he goes to the NewsFeed Activity.
The second time, after the settings filled, I want to launch the app with NewsFeed Activity.
I google it but could not find any detailed explanation or tutorial.
Its very simple.
You can use preferences to store if this first time then show Settings Activity else always show NewsFeed Activity.
Below is the sample code to use Preferences
SharedPreferences prefs = this.getSharedPreferences("MyAppPrefs", MODE_WORLD_0);
boolean settingsFilled = prefs.getBoolean("SettingsFilled", false);
if(!settingsFilled)
{
//SHOW SETTINGS ACTIVITY
SharedPreferences prefs = this.getSharedPreferences("MyAppPrefs", 0);
Editor editor = prefs.edit();
editor.putBoolean("SettingsFilled", true);
editor.commit();
}
Use the above code in your NewsFeed Activity.
I am writing a android application where I want to register my application to remoter server when application is first launched on installation. Application will register to remoter server itself without taking any user input. How Can I track whether this is a first application launch after installation ?
By setting a shared preference:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = prefs.edit();
edit.setBoolean("launched", true);
edit.commit();
You can check for it on each launch with:
boolean launched = prefs.getBoolean("launched", false);