Android: can't pass variable to a third activi

2019-09-19 01:52发布

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 :)

3条回答
啃猪蹄的小仙女
2楼-- · 2019-09-19 02:25

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:

SharedPreferences preferences = getSharedPreferences("preferences", MODE_WORLD_WRITEABLE);
preferences.edit().putString("MY_NOME",this.nome).commit(); 
preferences.edit().putString("MY_ID",this.id).commit();

In other Activities:

SharedPreferences preferences = getSharedPreferences("preferences", MODE_WORLD_WRITEABLE);
String myNome = preferences.getString("MY_NOME", null);
String myId = preferences.getString("MY_ID", null);
查看更多
劳资没心,怎么记你
3楼-- · 2019-09-19 02:39

you are putting your values in an Intent object, and then you created different Intent to start your last activity.

public void checkin (View v){

    Intent intent=getIntent();
  //first intent you created.

    String pkg=getPackageName(); 
    String nome=intent.getStringExtra(pkg+".myNome"); 
    String Id=intent.getStringExtra(pkg+".myId"); 
    intent.putExtra(pkg+".myNome", nome); 
    intent.putExtra(pkg+".myId", Id);
 //you are adding extra in first intent
    intent=new Intent(getApplicationContext(), entrata.class);
    // here you again recreated an Intent object second intent
    startActivity(intent);
 // you are start activity using second intent which do not contain any extra
}

Change this by::

 public void checkin (View v){

    Intent intent=getIntent();
    String pkg=getPackageName(); 
    String nome=intent.getStringExtra(pkg+".myNome"); 
    String Id=intent.getStringExtra(pkg+".myId"); 

    Intent intent1 = new Intent(getApplicationContext(), entrata.class);
    intent1.putExtra(pkg+".myNome", nome); 
    intent1.putExtra(pkg+".myId", Id);
    startActivity(intent);

}

查看更多
来,给爷笑一个
4楼-- · 2019-09-19 02:46

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:

 public class choice extends Activity {
     private String nome;
     private String id;     //first letter lowercase in variables: Java conventions
     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   
    this.nome=intent.getStringExtra(pkg+".myNome"); //this is now an instance variable
    this.id=intent.getStringExtra(pkg+".myId");     //this is now an instance variable
    intent.putExtra(pkg+".myNome", this.nome); 
    intent.putExtra(pkg+".myId", this.id);  
    TextView tvNome =  (TextView) findViewById(R.id.txtNome); 
    tvNome.setText(this.nome);
}
//pulsante per il checkin
public void checkin (View v){
     // l'intent di questa activity
    //Intent intent=getIntent();  //NO NEED FOR THIS ANYMORE
    //String pkg=getPackageName();//NO NEED FOR THIS ANYMORE 
    //prendiamo i dati   
    //String nome=intent.getStringExtra(pkg+".myNome"); //NO NEED FOR THIS ANYMORE
    //String Id=intent.getStringExtra(pkg+".myId"); //NO NEED FOR THIS ANYMORE
    //li reinseriamo nell'intent

    intent=new Intent(getApplicationContext(), entrata.class);
    intent.putExtra(pkg+".myNome", this.nome);
    intent.putExtra(pkg+".myId", this.id);

    startActivity(intent);
}
}

I think that is more optimized. If nome or id have not changed since onCreate it needn't to putExtra again. If it does, maybe onCreate is the wrong place to use the putExtra

查看更多
登录 后发表回答