blocking the activities once they are executed in android - android

I am working on an application which contain register activity as it's first page which is on tabs. i want that once the user is register then whenever the user starts the application it should always run from main menu screen and should never display the register screen till the user uninstall the application and reinstall it again.

you can use SharedPreferences. This is an example:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
So you can check if firstTime is true doing this:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
if(mPrefs.getBoolean(firstTime, false){
//show screen
}

Set your AndroidManifest to start the MainActivity as the default activity.
Then in the onCreate check if the user has registered (perhaps store this in SharedPreferences), if they have NOT registered - instantly start the intent for your RegisterActivity, otherwise carry on as normal.

Related

When to show dialogbox [duplicate]

This question already has an answer here:
Detect if an app is started/resumed from 'outside' the app
(1 answer)
Closed 6 years ago.
So my Application has an Activity A and few other Activities B,C,D etc, all of which opens on different clicks from Activity A.
My requirement is to show a "Enter Pin" DialogBox every time app is opened from outside like (starting app first time, unlocking screen, resuming app after it was paused and other apps were used).
But Dialogbox should not show up when i open and close other Activities (B,C,D etc).
But in both cases, onPause->onStop->onRestart->onResume is called.
So how should i distinguish that whether the user has gone outside the app or not and where should i place my dialog.show()
In Activity A do like this,
#override
onCreate(){
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("isRunning", true);
editor.commit();
}
#override
onStop(){
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putBoolean("isRunning", false);
editor.commit();
}
and in onResume() check the pref and if true then show your dialog.
#override
onResume(){
SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
if(pref.getBoolean("isRunning",false)){
//Show your dialog here
}
}
NOTE : I apologize It's directly written in editor, I'll edit this after being with IDE, But use this logic.

Android - How to change the launch activity?

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.

Is there any way to check if user has registered once then app should diplay menu directly. If not then above verification screen should be displayed

I have registered a user by sending a verification code at once in whole life cycle.
I want to show user menu directly if he is a registered user.
If not then registration screen should be displayed.
Can you tell me how to do that?
How can I check if user is registered or not and display screen accordingly?
You can store registered boolean value into preferences.
http://developer.android.com/guide/topics/data/data-storage.html#pref
Whenever your user register in your app with verification code store it in sharedpreference.
using this
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString("verification_code", "your code value");
editor.commit();
then whenever user again come to your application on the first screen of your application check
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String code= sharedPreferences.getString("verification_code", "");
if (code=="") {
"register activity"
} else {
"menu activity"
}
I hope this will help you.
http://developer.android.com/guide/topics/data/data-storage.html
you can find other options here. But this is the easiest way to do.

Resetting shared preferences upon reinstallation

Is there any way to delete or reset shared preferences data when re-installing an app, without uninstalling the app and installing it?
What I mean to say is, currently, I am developing an app which uses shared preferences, but as I am still developing it, I keep running and uploading the app to the test phone through Eclipse after I make changes. Currently I am unable to run the app from the very beginning of its expected process ( after the first time) , without uninstalling the older version and then uploading the app again.
For this in your launching activity onCreate() method check whether the shared preference file exist or not if it is exist delete it.and later create it where ever you want..
you can check the preference file exist or not like this..
public boolean isFirstTime() {
return getDatabasePath("your file name").exists();
}
Clear the activities like :
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Clear ur shared Pref :
SharedPreferences pref = this.getSharedPreferences("mypref", Context.MODE_PRIVATE);
getSharedPreferences("pref", Context.MODE_PRIVATE).edit().clear().commit();
Check in your launch activity whether to clear the preferences or not with a function like this:
SharedPreferences prefs = this.getSharedPreferences("prefs", Context.MODE_PRIVATE);
if (!prefs.getBoolean("FirstRun", true)) {
// Not the first time so clear prefs
prefs.edit().clear().commit();
} else {
// Set the value for future runs
prefs.edit().putBoolean("FirstRun", false).commit();
}

Android register to remote server on first application uses

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

Categories

Resources