I have three activities:
- login
- choice
- entry
I Must pass the var "Name" from login to choice (and this work well) and then,from choice to entry,and this is where i have the problem :/ I can pass name to choice,but when i try to pass it to entry,i can't! It's strange because if I pass the variable directly from login to entry, it works :/ So:
- login –> entry works!
- login –> choice works!
- choice –> entry not works!
This is the code to pass from login to choice
Intent intent;
String pkg=getPackageName();
intent=new Intent(getApplicationContext(), scelta.class);
//inseriamo i dati nell'intent
String parts[] = risp.split("/");
intent.putExtra(pkg+".myNome", parts[0]);
intent.putExtra(pkg+".myId", parts[1]);
startActivity(intent);
this is choice (where probably is the error):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scelta);
// l'intent di questa activity
Intent intent=getIntent();
String pkg=getPackageName();
//prendiamo i dati
String nome=intent.getStringExtra(pkg+".myNome");
String Id=intent.getStringExtra(pkg+".myId");
intent.putExtra(pkg+".myNome", nome);
intent.putExtra(pkg+".myId", Id);
TextView tvNome = (TextView) findViewById(R.id.txtNome);
tvNome.setText(nome);
}
//pulsante per il checkin
public void checkin (View v) {
// l'intent di questa activity
Intent intent=getIntent();
String pkg=getPackageName();
//prendiamo i dati
String nome=intent.getStringExtra(pkg+".myNome");
String Id=intent.getStringExtra(pkg+".myId");
//li reinseriamo nell'intent
intent.putExtra(pkg+".myNome", nome);
intent.putExtra(pkg+".myId", Id);
intent=new Intent(getApplicationContext(), entrata.class);
startActivity(intent);
}
checkin is the method that I use when i tap on the button for pass from choice to entry. And this is where i take name in Entry:
Intent intent=getIntent(); // l'intent di questa activity
String pkg=getPackageName();
String nome=intent.getStringExtra(pkg+".myNome"); //prendiamo i dati
TextView tvNome = (TextView) findViewById(R.id.nome);
tvNome.setText(nome);
thanks to everyone :)
You can also use SharedPreferences for this. Define a SharedPreferences object in your Activity. Put the data you want in LoginActivity. Get data from SharedPreferences object in each Activity you like:
In Login Activity:
In other Activities:
you are putting your values in an Intent object, and then you created different Intent to start your last activity.
Change this by::
}
Maybe has something to do with asking for the extras twice (onCreate and checkin methods). You could try to retrieve the extras only inside the onCreate method and store the values in instance variables (to share this with all the methods in the instance). Something like:
I think that is more optimized. If
nome
orid
have not changed since onCreate it needn't to putExtra again. If it does, maybe onCreate is the wrong place to use theputExtra