copiar cadena de actividad a otro Android
Frecuentes
Visto 323 equipos
1
at begining i want to say sorry for my bad english i hope u understand me. I want to copy one string to another activity so i create :
package com.example.kliker;
import android.app.Application;
public class GlobalClass extends Application{
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public String getEmail() {
return email;
}
public void setEmail(String aEmail) {
email = aEmail;
}
}
and in activity to set i use:
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
globalVariable.setEmail("1");
globalVariable.setName("1");
and when i want get:
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
a = globalVariable.getEmail();
b = globalVariable.getName();
mTextView.setText(a);
But it doesn't work ? I should refresh activity or something like that ?
http://speedy.sh/dvt94/Desktop.rar Manifest,activity from i get and set, activity set, activity get
I would like to make one activity outstay data about category and food also i want to make another activity when we are in it and when we click on food it send informations about itself category and chosen food, main activity download that information and by means of them it build graphic sentence
3 Respuestas
0
Use Bundle API - http://developer.android.com/reference/android/os/Bundle.html.
In your A Activity -
Intent intent = new Intent(A.this, B.class);
Bundle bundle = new Bundle();
bundle.putString("value", "String Value");
intent.putExtras(bundle);
startActivity(intent);
And, in B Activity -
Intent i = getIntent();
Bundle extras = i.getExtras();
String value = extras.getString("value");
You would be interested in this http://hmkcode.com/android-passing-data-to-another-activities/
Respondido 05 Feb 14, 17:02
Yes i heard about it but what if i have a lot of activity ? I dont know... If i doesnt have another option i use it - KariiO
for such requirement we use Shared Preferences Class with static getter/setter and variables (these are suggested by @user1676075 also) - canaco sony
0
You are instantiating your GlobalClass class twice so you are getting different variables. Try something like this in the same activity and see if it works.
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
globalVariable.setEmail("1");
globalVariable.setName("1");
a = globalVariable.getEmail();
b = globalVariable.getName();
mTextView.setText(a);
Respondido 05 Feb 14, 17:02
I dont want get and set in same activity - KariiO
0
I can think of at least three decent choices:
1) Use a singleton (pros - simple; cons - won't persist across application lifetime).
2) Use SharedPreferences (easy enough to research); if you're really using it for something like username for the application (which is what it appears from the example) then SharedPreferences are appropriate.
3) Use an Intent to trigger the next activity from your current one, and pass the string as an extra (intent.putExtra(...), intent.getStringExtra(...)).
Respondido 05 Feb 14, 17:02
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android variables android-activity global or haz tu propia pregunta.
can you show your activity? - Jerry Wattre
speedy.sh/dvt94/Desktop.rar - KariiO