I want to create one time activity - android

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

Related

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.

How do remember that user signed up and take straight to another Activity?

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.

An one-time introductory slideshow for all users

how do i do it:
The first time each user visits the Apps screen, a slide show will appear instead of a splash screen which will tell the user about various features.
4 slides will come on the very first run of the app. slides will be changed using the swipe gestures
the slideshow will appear only for one time after the very first download.
Check this image as a reference: http://main.makeuseoflimited.netdna-cdn.com/wp-content/uploads/2012/03/HiLauncher6.png?323f2c
All you have to do is store a Boolean variable inside the app using SharedPreferences which will determine whether the slideshow has appeared once or not, if so the don't show it again.
For example:
SharedPreferences sp = getSharedPreferences(MY_PREFS_NAME,
Context.MODE_PRIVATE);
boolean isFirstTime = sp.getBoolean(IS_FIRST_TIME, true);
if (isFirstTime) {
showSlideshow()
} else {
// Go to your main activity
Intent main = new Intent(Start.this, MainActivity.class);
startActivity(main);
finish();
}
Don't forget to set isFirstTime to false once the slideshow has been shown, and store it in the sharedPreferences
Hope that helps

How to close app in splash screenn in android

I want my app should be close on splash screen. Actually the whole scenario is from server side if app is blacklisted then app should not open complete, it open show splash screen and show dialog that "you are not authorized " then on click of ok button app should close.. It is not working.. on splash screen why? It is working on Main Activity only
I suggest you should use SharedPreferences and save the state like a boolean isAuthorized set it to true then in your SplashActivity do the following:
SharedPreferences prefs = getSharedPreferences("PREFS_NAME", 0);
boolean isAuthorized = prefs.getBoolean("isAuthorized", false);
if(isAuthorized)
{
startActivity(new Intent(Splash.this, MainActivity.class))
}
else
{
finish(); // or show a dialog that prompts user that you are not authorized....
}
If you have never used SharedPreferences follow this link.
Hope this will help you.

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.

Categories

Resources