I have 3 buttons in main activity, this 3 buttons opens another activity, it is easy, medium and hard game type. i want to get the scores of each level of difficulties and store it in main activity, any example for this one? any help would greatly appreciated thank you!
The easiest way to do this would be to pass variable you want to share to the main activity in the Intent you're using to start the activity:
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("EXTRA_SESSION_ID", variable);
startActivity(intent);
Access that intent on next activity
String variable= getIntent().getStringExtra("EXTRA_SESSION_ID");
The docs for Intents has more information (look at the section titled "Extras").
You can store variable to any variable you want in MainActivity and use it.
You can create a Util Class, in that Class create a static variable named difficulty.
Assign it according to Button pressed.
Inside OnClickListener assign it, later retrieve it.
Thanks.
Setting values in Preference:
SharedPreferences.Editor editor = getSharedPreferences("MyPref",
MODE_PRIVATE).edit();
editor.putInt("begginer", 120); // put begginer level score here
editor.putInt("medium", 37); // put the medium level score here
editor.putInt("hard", 12); // put hard level score here
editor.apply();
To Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences("MyPref", MODE_PRIVATE);
int begginer_score = prefs.getInt("begginer", 0); //0 is the default value.
int medium_score = prefs.getInt("medium", 0); //0 is the default value.
int hard_score = prefs.getInt("hard", 0); //0 is the default value.
Related
I have 5 activities, say activity A,B,C,D and E.
Each activity has two buttons yes & no,buttons have the data which I want to pass to activity E only.
I need to do following things:
--> When user press yes/no button of A_activity, the user move to B_activity but data passed to activity E via intent.
Similarly at activity B user press yes/no button, user will move to activity C but data passed to activity E and so on.
I have done lot of search but couldn't find solution is there any way to do this.
Use SharedPreferences class to pass the data:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("key", "Value");
editor.commit();
Then in the Activity E:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String value = sharedPref.getString("key", defaultValue);
Hope this helps.
You can indeed pass data back through an intent to an Activity's onActivityResult()
Activity E starting Activity A
final int RESULT_FOR_CLASS_DATA = 12; // pick a number to use
String returnedData;
Intent intent = new Intent(this, ActivityA.class);
startActivityForResult(intent, RESULT_FOR_CLASS_DATA);
Example in A (started from E)
Intent data = new Intent();
data.putExtra("ReturnData", dataToReturn);
setResult(RESULT_OK, data);
finish(); // returning to Activity E
Example in onActivityResult() of E
if (requestCode == RESULT_FOR_CLASS_DATA) {
returnedData = data.getStringExtra("ReturnData"));
}
There's a wide variety of ways to accomplish moving data around though so this is just one example.
To recommend an alternative, use Handler instead of this. Supposed that since the return Activity is paused, handler might require setting data in a separate class or global static variables, etc. and retrieved on Activity restart or something. So its possible to do with little to no code duplication in any class. Just retrieve and use. Whereas Intent requires code duplication in every class. It would also easily allow going from any Activity to any Activity like A->B->E->C instead of requiring E->A->E->B. Just something to think about.
I'm developing an app which adds two numbers. The user provides the numbers via EditText and the result is displayed via TextView. What I want to do is to save the values of the numbers entered by the user and the result via button (to see them whenever the user wants) and display them in the layout of another activity (whithout EditText's). Remark that the user would be able to see the results saved whenever he/she wants.
Hope you can help me. Thank you so much.
There is multiple ways to achieve that :
Method 1:
Use static class setter and getter method:
create static class and set values from first activity and get value from second activity
Method 2:
Post your values through the intent
Method 3:
Use database to store data from one activity and get data from other activity
Method 4:
Use Shared preference
From Your question what i understand::
Your first editText1 has value1
Your second editText2 has value2
textView1 has the result of value1 and value2
Solution:: Get the values from the views and use intents to pass the data between activities
Code::
In your current Activity, create a new Intent: - OnCreate()
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("editText1",editText1.getText());
i.putExtra("editText2",editText2.getText());
i.putExtra("textView1",textView1.getText());
startActivity(i);
Then in the new Activity, retrieve those values: - OnCreate()
Bundle extras = getIntent().getExtras();
if (extras != null) {
String editText1= extras.getString("editText1");
String editText2= extras.getString("editText2");
String textView1= extras.getString("textView1");
}
Use the variables editText1,editText2 and textView1 in second activity to set the values to any views as u wish
HOPE THAT HELPS, let me know if you face any problems in debugging
make use of SharedPreferences like this:
SharedPreferences sp= getSharedPreferences("ttt", 0);
SharedPreferences.Editor editor = sp.edit();
String etValue=(EditText)findViewById(R.id.yourEditTextId).getText().toString();
String tvValue=(TextView)findViewById(R.id.yourTextView).getText().toString();
editor.putString("etValue", etValue);
editor.putString("tvValue", tvValue);
editor.commit();
// in anywhere user wants
SharedPreferences settings = getSharedPreferences("ttt", 0);
String yourEditTextValue=settings.getString("etValie", "");
String yourTextViewValue=settings.getString("tvValie", "");
I am novice programming on Android. I have one question, I have 2 activities. Firs I pass a parameter from the Activity A to the B like this:
Intent intent = new Intent(getBaseContext(), ActivityB.class);
intent.putExtra("ALMACEN_ANTES", almacen.getText().toString());
startActivity(intent);
And now in the activity B y get the extras. Later since the Activity B, I pass parameters to the A too.
My question is, is it possible to pass parameters to another activity without doing
Intent intent = new Intent(getBaseContext(), ActivityB.class) because I don´t want to open 2 times the same activity.
Thank you!
Yes . You can save the value using SharedPreferences
It will be stored in an internal xml file and you can save it and retrive whenever you want. It need not start the activity when value passing.
An example shows below
SharedPreferences sharedpreferences;
Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.putString(Phone, ph);
editor.commit;
you can retrive this value from the activity where you need to access these values. It can be done by:
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String name=sharedpreferences.getString("Name","");
String ph=sharedpreferences.getString("Phone","");
You want to update a value in the activity A , you mean? because if you don't open it why sending a value to it ?
you can share data between two activities by other ways. In your case the best one would be a Singleton class:
you create a class which is inheriting from Application and containing your data, and from every activity you can update you data or get it...
Your singleton class:
import android.app.Application;
public class MyApplication extends Application {
private String data;
public String getData() {return data;}
public void setData(String data) {this.data = data;}
}
From activity B , you can set your data:
MyApplication app = (MyApplication) getApplicationContext();
app.setData(someData);
And teh Ativity A can have them like this:
MyApplication app = (MyApplication) getApplicationContext();
String data = app.getData();
See this answer
My question is how to pass data like String between two activities. Normally I would do this:
Intent i = new Intent(thisclass.this,NextClass.class);
Bundle b = new Bundle();
i.putExtras(b);
b.putString("Name",Name);
StartActivity(i);
But this would make my Activity close and will open the next Activity, no? Is there any way that I can only pass data without opening the other activity?
I think you are looking for something with the SharedPreference see the documentation : SharedPreference
if it is what are looking for.
Try this:
Global:
private SharedPreferences pref;
onCreate:
pref = this.getSharedPreferences("SharedPreference", Context.MODE_PRIVATE);
The place where you gonna save your data:
String data = "yourData"
pref.edit().putString("myData", data).commit();
And the other Activity:
Global:
private SharedPreferences pref;
onCreate:
pref = this.getSharedPreferences("SharedPreference", Context.MODE_PRIVATE);
The place where you gonna take your data:
String dataFromFristActivity = pref.getString("myData", null);
Your calling activity does not close but is paused.
I am with #Egor on this. The called activity does not yet exist. Unless u are trying to send data between activities in two different apps. That is a whole different can of worms.
I developed one app in that I want to send URI from Class1 editText to another class containing editText.
Can anyone tell me how to do that?
SharedPreferences are the wrong way to do that. Use the Bundle feature every Intent can have: http://developer.android.com/reference/android/content/Intent.html
On the second activity you can call getExtra() and there you go...
Assuming you want to use the SharedPreferences to transfer the URI, you could try this:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putString("my-uri", "http://google.com/").commit();
And to retrieve the URI:
prefs.getString("my-uri", "default URI");
If your two classes are Activities, and if one of them starts the other one, you should probably pass the URI as an intent extra.
Also, read the FAQ and accept some answers!
you can use System.setProperty/get Property as well.
don't you like to add putExtra in intent
like this
Intent i = new Intent(getApplicationContext(), Audit_FSD_Tab.class);
i.putExtra("UsrID", UsrID);
i.putExtra("Store", Store);
i.putExtra("location", location);
startActivityForResult(i, 0);
now in other activity access these extra
Bundle UsrVal = null;
UsrVal = this.getIntent().getExtras();
UsrID = UsrVal.getString("UserId");
Store = UsrVal.getString("Store");
location = UsrVal.getString("location");
Try to store Uri in the edit text inside shared preferences in first activity and then on create method of second activity retrieve Uri value from the shared preferences and display that in edit text.simple...
It can be possible by using Shared Preferences, for example
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
data=pref.getString("key_name5", null);
editText.setText(data);
You can follow tutorial here
http://firstcode.info/android-sharedpreferences-basics/