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.
Related
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", "");
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;
can someone help me about this one? I'm trying to build a simple quiz app that uses button, checkbox and textview. I want the score of all questions appear in the last page (5 pages) but can't make it work. I want to get the score from the previous question and then send it to the result page.
Store them in SharedPreference and get the value in the last page and/or clear it depending on what you need to do.
You should be able to pass the score from one activity to another using bundle in your intent.
//From Activity A
//To pass data from one activity to another
Intent i=new Intent(context, MyTargetClass.class);
i.putExtra("somekey", scoreValue);
context.startActivity(i);
//To Activity B
//To retrieve data from an activity
Intent intent = getIntent();
String scoreValue = intent.getStringExtra("someKey");
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.
Intent intent = new Intent(this, Page.class);
intent.putExtra("arg", getText);
startActivity(intent);
//second activity:
String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);
To get value from one activity to another activity without using
intents....?? I want to show edittext passed data in other activity
edittext but without using intent...?? Kindly help me is it possible
or not....??
There are many ways: event buses, SharedPreferences, through global object (application class), database, simple file. But why?
Try direct and readable style first.
if you can't bundle the values you want to save in the Intent, then you could use the SharedPreferences. More information here