I have a username unique to every user that I want to send to a certain activity, but I don't want to use intents:
//create an intent and sends username
Intent intent = new Intent(RegisterOwner.this, Owner.class);
intent.putExtra("USERNAME",usernam);
startActivity(intent);
I want to send data to an activity without going to that activity, startActivity(intent); makes me go to the activity; I don't want that.
I just want to send data without starting the other activity.
I guess you want to have a particular data available between activities. One of the methods to achieve this is by making use of the SharedPreferences.
From your first activity(RegisterOwner)
SharedPreferences mySharedPreferences = this.getSharedPreferences("MYPREFERENCENAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("USERNAME",usernam);
editor.apply();
Once you do this the username is stored in the shared preferences. Now you have this data available throughout your app.
So from the Owner activity you can retrieve this as follows:
SharedPreferences mySharedPreferences = this.getSharedPreferences("MYPREFERENCENAME", Context.MODE_PRIVATE);
String username = mySharedPreferences.getString("USERNAME", "");
Related
I need to parse a single text from my MainActivity.java to all other activities in my app.
Is it possible to do so?
Just store the text as string in shared preferences and then get the string in other activities.. or you can also use broadcast receiver in all other activities. But first all the activities should call the receiver first then the MainActivity can send the text.
In MainActivity,
this.getSharedPreferences("MyPrefName", Context.MODE_PRIVATE).edit().putString("parsetext","yourtext").apply();
and in the other activities..
this.getSharedPreferences("MyPrefName", Context.MODE_PRIVATE).getString("parsetext","");
Better way is to pass data between activities. Code:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("parsetext", "your text here");
startActivity(intent);
Access that intent on next activity:
String s = getIntent().getStringExtra("parsetext");
Your question sounds you need global access of some data in your all Activites, whenever something is needed to be accessed temporarily that is, for the time until application is active you can use your Application class, which is globally accessible through getApplicationContext() in all Activites.
For reference see this answer:
https://stackoverflow.com/a/1945297/4878972
But if the data you need to access in all the Activites needs to get saved permanently then you can go for Shared Preference approach.
You can have a public static final String in your MainActivity and access that String from another activity. As in:
In MainActivity.java
public static final String MY_STRING = "my string";
In other places where you need to access the variable, you can access is as:
String string = MainActivity.MY_STRING;
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 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.
In the last activity of my app there is some numbers displayed in edit text's and an option to add more to these totals, the add more button takes you back to start of the app, where the user does the same thing again inputs some numbers and ends up back on last activity.
What I want to do is if they press the add more button is save the numbers that are in the edit text's so I can add them to the new set of numbers coming in.
What I thinking is save the numbers in on Pause, but don't know how and then add them to new numbers in on Resume, am I on the right track, what is the best way to do this.
In the "source" activity do this:
SharedPreferences settings = context.getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key", value);
editor.commit();
In the target activity do this:
SharedPreferences preferences = getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);
String value= preferences.getString("key", "");
The second parameter in getString is the default value you want returned if "key" isn't found.
Also Check these links
Android Using SharedPreferences to save and retrieve values - Counter
http://android-er.blogspot.in/2011/01/use-getsharedpreferences-to-retrieve.html
You can use Intent to do so. For example if you want to transfer an int.
//first activity
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("myInt", number);
//Second Activity
Intent intent = this.getIntent();
int number = intent.getExtra("myInt", -1);
Try implementing onSaveInsance and onRestoreInstance of the activity. This would be called before onPause and before onResume.
Sample is here.
Also, to add the data from a different activity which would return to this activity then you can use the startActivityForResult and send the data as intent from the new activity before finishing..
you'd better use SharedPreferences....
http://developer.android.com/guide/topics/data/data-storage.html#pref
You can use startActivityForResult and send your data via Intent.
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.