How to direct user from a splash activity to the main activity - android

I am asked to develop an Android application whose description is this:
Let’s assume if you like Football or Cricket so much and you can find
some unknown having the same interests.
You can send Hello to him/her and can chat as well. Fantastic. Let’s
Jump into the Project Ideas
You can Select the Interest from the menu
Find the person with the common interests and send him Hello request
You can get so much information about your interests.
Chat with Friends
You can build this App using Android Studio.
So I have made a splash screen, register, login page and a user setup activity till now.
I have also designed a dashboard type interface which would display various kinds of interest for the user to choose from. So I would have to make a different main activity for different interests, like if the user chooses sports as interest, the main activity would be different than for the user who choses singing as his/her interest.
What do I do if I want to direct the user from the splash screen to the main activity screen (sports, singing, etc.) of his/her choice? (Example code would be appreciated.)

You can use Shared Preference for this.
Just save your interest in preferences with some tag when you select it like below:
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
myEdit.putString("name", name.getText().toString());
myEdit.putInt("age", Integer.parseInt(age.getText().toString()));
myEdit.apply();
myEdit.commit();
Then you can get it on Splash screen from shared preference like:
// Make sure to use the same name while getting data that you use while saving it.
SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_APPEND);
String S1 = sh.getString("name", "");
int A = sh.getInt("age", 0);
You can now use S1 and A variables for whatever you want.
In your case, you can use if/else or switch.

Related

android preference settings range value

I am implementing a preference activity so I can let choices to the user about what my app can and can't do and to define a user profile so I can change the way I request data.
I have one part where I need to show places to the user (restaurant, stores, etc) so I would like to be able to have a "range widget".
Let's say :
1 = poor
2 = cheap
3 = average
4 = rich
I want the user being able to say I want only store between 1 and 3 so it would need to be a widget with a double "circle" allowing the user to create a range.
Any idea of something like this existing ? (I am pretty sure I already seen something like this in the past but I can't remember where)
Take a look at RangeBar. It sounds exactly like what you need

Persistent login on most important app

I was wondering how the most used apps keeps the login?
When I open Facebook, Twitter, Instagram or 500px my apps are ready without no time of login and I need to know the how-to for my apps, who request 8-10 seconds to login and download the user list.
EDIT: I'm not talking about the "cookie" for credentials. If you try Facebook, for example, you can see that the first login took 30 seconds, and then it is always connected. The other time you open Facebook, it doesn't ask for credentials for Shared Preferences, but it doesn't take 30 seconds to login. Why?
I believe all the Above posts are accurate but i wanted to add my two cents. When your application launches you should make a call in your Launcher Activity (activity that launches when your app icon is clicked on home screen if its not already open) to
// This will get you an instance of your applications shared preferences.
SharedPreferences preferences = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
// Values
String userName = preferences.getString("username",null);
String password = preferences.getString("password",null);
// Then you want to query preferences for values such as username and password like
if((userName != null) && (password != null))
{
// **** Log your user into your application auto**Magically** ********
// ------------------- Option 1 - web request -------------------
// First i Would make the initial Web Request before trying to send my User into
// a new Activity.
// Run an `AsynchTask` against your webservice on the server if this is something
// you need to do to see if the username and password, are correct and registered
//---------- Option 2 - Check the SQLite Database(if you are using one) ---------
// Otherwise you can use this info to read from an SQLiteDatabase on your device.
// To see if they are registered
// This is where you would create a new intent to start
// and Login your user, so that when your application is launched
// it will check if there are a username and password associated to the
// Application, if there is and these are not null do something like
// Create a new Intent
Intent automagicLoginIntent = new Intent(getBaseContext(),AutomagicLogin.class);
// Pass the Bundle to the New Activity, if you need to reuse them to make additional calls
Bundle args = new Bundle();
args.putString("username", userName);
args.putString("password", password);
automagicLoginIntent.putExtra("UserInfo", args);
// Launch the Activity
startActivity(automagicLoginIntent);
}else{
// Show the Layout for the current Activity
}
This should be done in your onCreate(Bundle savedInstanceState); method but that is the short of it. It should not be too tricky to implement but the implementation is dependent on how your logic works.
Do you read from SQLIte Database ?
Do you read from WebService ?
etc.
But this should be all you need, unless you need to store some other values in the prefs.
Side Note
All the code above will not work if you have no way of getting this information from the user in the first place, i.e. a registration form.
When thinking of registration, you can initially store these values with the following code:
// Inside an Listener , get a reference to your `TextView`s that were used to enter
// username and password
TextView usernameText = (TextView) findViewById(R.id.textViewUsername);
TextView passwordText = (TextView) findViewById(R.id.textViewPassword);
// Get a reference to the SharedPReferences, SO WE CAN BEGIN TO STORE THE VALUES ENTERED
SharedPreferences preferences = getBaseContext().getSharedPreferences("MyPrefs", SharedPreferences.MODE_PRIVATE);
// Need this to Edit Preferences
SharedPreferences.Editor editor = preferences.edit();
// Then you can add the values
editor.putString("username",usernameText.getText().toString().trim());
editor.putString("password",passwordText.getText().toString().trim());
// Must Call this to write values to the Preferences
editor.commit();
And that should be all you need, to initially store the values and read them back from preferences to automagically login your user each time ur application is launched if it has not been opened, if it has been opened android activity lifecycle should reload the current activity that was last paused.
Good Luck!
You should save an authentication token for each user, you don't need to explicitly reauthenticate every time the user opens the app. When the app makes subsequent API calls, use that authentication token and send it to your back-end. If the back-end tells you that your authentication is invalid, then you know you need to reauthenticate.
They store the token using sharedPreferences
http://developer.android.com/reference/android/content/SharedPreferences.html

Trying to import a SharedPreference of values?

I used SharedPreferences in one activity to basically store a bunch of data in a HashMap like method. In particular, app names (String) and their respective sorting as either a Category 1, 2, or 3 app. Stuff like:
editor.putString("Cut the Rope", "Category 1");
In another activity, I want to use this SharedPreference to create an ArrayList of only the Category 1 apps. Here is how I thought this could be done:
ArrayList<String> cat1Apps = new ArrayList<String>();
SharedPreferences stored = getSharedPreferences("Sorted Apps", 0);
SharedPreferences.Editor editor = stored.edit();
Map<String, ?> mappedPreferences = stored.getAll();
for(Map.Entry<String, ?> entry: mappedPreferences.entrySet()){
if(entry.getValue().toString().equals("Category 1")){
cat1Apps.add(entry.getKey());
}
}
For some reason though, this isn't working. I'm taking these app names and then later there's some code that creates a GridView display of Category 1 apps. If I just define cat1Apps.add() a bunch of time, this works fine, but if I try to import through the SharedPreferences as above, it doesn't create the GridView.
The problem here is that editor.commit() was never inserted after after adding the Strings to SharedPreferences so they were never saved. Otherwise, this code to retrieve the SharedPreferences was correct.
Some debug links as asked for by the OP
This one isn't necessarily about debugging but has some good information
Eclipse Debugging
YouTube video onDebugging- Note I just scrolled through some of this but looks like it could be helpful
Also not that when they say, "Right click and click toggle breakpoint", you can also double-click in the margin and you should see a green dot which means there's a breakpoint there. Double-click the green dot to remove the breakpoint.

Android - ImageButton Values / SharedPreference

If you have time , please can you take some time for me ? I really need some help.
Let me explain;
Im working on an android app.
There is a layout and it has 5 ImageButton and a webview. When the users click on a imagebutton, without problem it calls a website below..but more or less i have 20 web site.i want to add an option for users..for example the user will choose a web site from Prefs. screen then automatically one of imagebuttons values (i mean icon AND its loadurl function) will change.
I created Pref Screen and i can see my website in this which i wrote in array.xml
but totally im not able to set them to Imagebuttons..
im beggin u.its our last curve..then it ll finish.
Im tried to use this code :
Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
but i couldnt.
Please explain me step by step clearly.
also i dont want that only for me,on internet there is no source for this issue.im searching and trying everything what i can think more than 6 days but nothing.
Thank you so much
SharedPreference Data = getSharedPreferences(filename, 0);
SharedPreferences.Editor e = data.edit();
e.putString("website", websiteVariable);
e.commit();
Basically what this does is allows you to store user information such as scores for a game, stats, and other variables.
The first line gets reference or creates the file to write the data to.
The second line allows you to edit the file to write new information to it..
e.putString()
Takes two parameters the first one being the Key to pull the value you out later, and the second being the value you want to put into the file.
The last line commits the data so that it is saved to the file.
You can get more info from the docs here
Also if you want to pull the data out just do
SharedPreference Data = getSharedPreferences(filename, 0);
String value = Data.getString("website"); // use the key here to pull the data out
EDIT:
So for example if the user selects a certain image you could use a key to refer to each image and get the value later to decided which icon the user picked before.

Android SharedPreferences

I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,
Accessing data from SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");
if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice.
use this condition as you wish
Saving data in SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();
I hope it helps.
Save the user's preference as default-engine=google by default in a shared preferences file.
On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.
Hope this helps.

Categories

Resources