When to show dialogbox [duplicate] - android

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.

Related

I want to create one time activity

I want to create one time activity window to my Android app.
In the window have two buttons.the buttons are connect to two different pages.
If you download app and run for the first time, you see this window first.then you have to select one button.
When you click the button, you will connect to the target page.
If you close the app, and open it again you can't see the window with two buttons.
The only thing you can see, the window that you previously selected with the button.
This is the thing, I want to do.
I tryied many codes based on YouTube videos.
But I didn't get the progress, that I want.
This the demo app for my course project.
I stuck with this problem.
you can download my project :-
Dropbox - https://www.dropbox.com/s/r4iztx8onqu2ctz/demo.zip?dl=0
Zippyshare - https://www85.zippyshare.com/v/bnXekEoz/file.html
You can save a bit in SharedPrefrence "isFirstRun" set it to true when the application is first run and go through your first activity.. for future run you can check if the "isFirstRun" bit is true skip the first activity.
More about SharedPrefrences here.. https://developer.android.com/reference/android/content/SharedPreferences
In your MainActivity :
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if (firstRun) {
startActivity(new Intent(getApplicationContext(), IntroActivity.class));
}
Create new Activity and named IntroActivity,and in Onclick Button :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.apply();
finish();
}
});
What you could also do is use install referer receiver then store the value in shared preferences and use that once you open activity https://developers.google.com/android/reference/com/google/android/gms/tagmanager/InstallReferrerReceiver

Start application with second activity

In my android app, When user have successfully login with Facebook and Google Plus then user have one confirmation Activity which have next button in disable mode. While, admin will be enable user from database. If user will be enable from database by admin then confirmation Activity and next button will be enable and user can move to next Activity. When user open app in second time then user will be able to show the confirmation Activity.
I don't know, how to apply this logic :
Scenario:
First Scene:
Splash screen->Login->success->confirmation activity
Second Scene:
ConfirmationActivty->Next button enable
Assuming SplashActivity.java is your main activity
Change your code as shown below
public class SplashActivity extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor prefEditor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = getSharedPreferences("MyPreference", MODE_PRIVATE);
// returns true if value does not exist
// if it's users first time this will return true
if( preferences.getBoolean ("isFirstTime", true)){
prefEditor = preferences.edit();
// changes the value
prefEditor.putBoolean("isFirstTime", false).commit();
}else{
// add your code to open your Confirmation Activity
finish();
return;
}
// rest of your code including `setContentView()`
}
}
You can use SharedPreferences, or File or Database for user's history data and check if the user fulfill your criteria. Then you can let him move from one activity to another. Check these links
SharedPreferences Totorial
SQLite & Content Providers
You can set the visibility of "next" button to "GONE" and make a network call in the background to your server to check if the admin has approved or not. If approved, you can set the visibility of the button to "VISIBLE". And during the network call you can start a progress dialog.Click here to see how to use visibility.
[Update]
you can use something like:
final SharedPreferences prefs = getApplicationContext().getSharedPreferences(
Constants.LAUNCH_TIME_PREFERENCE_FILE, Context.MODE_PRIVATE);
editor = prefs.edit();
profile = getApplicationContext().getSharedPreferences(Constants.PROFILE_PREFERENCE_FILE,Context.MODE_PRIVATE);
if (!prefs.getBoolean(Constants.FIRST_TIME,false)) {
// <---- run your one time code here
Intent intent = new Intent(this,first_screen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
For First Scene :
You should set flag i.e isUserLoggedIn in preferences which will false by default.
After user logged in, set this flag to true.
In LoginActivity onCreate(), check that flag from preferences, if it is true finish the activity and start confirmationActivity else do nothing.
For Second Scene :
Here, you can also maintain the flag isUserConfirmed same as for login. And you can send notification from server to the user, after receiving notification update the flag to true. And enable the button on ConfirmationActivity if the activity is in the foreground.

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.

blocking the activities once they are executed in 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.

Categories

Resources