问题
I have made an application with 5 activities-calculator1.java,calculator2.java,deposit.java,calculator3.java,calculator4.java respectively,In activity 1 there is a toggle button and a next button,and all the other activities contains a next button,Now i want is when toggle button is pressed and next button pressed then it should goto calculator2.java,and from calculator2.java,when i press next button,if the "toggleButton" of calculator.java is "on" it should go to deposit.java else go to calculator3.java,
My code is as below:(on next button click):
Calculator1.java
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Calculator_1Activity.this, Calculator2.class);
intent.putExtra("toggleBtn", tg.isChecked());
startActivity(intent);
}
});
Calculator2.java
btn2.setOnClickListener(new OnClickListener() {
final boolean val= getIntent().getExtras().getBoolean("tg");
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(val==true){
Intent iq=new Intent(Calculator2.this,Deposit.class);
startActivity(iq);
}
else{
Intent ir= new Intent(Calculator2.this,Calculator_3Activity.class);
startActivity(ir);
}
}
});
Deposit.java
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent iy = new Intent(Deposit.this,Calculator_3Activity.class);
startActivity(iy);
}
});
Calculator3.java
btn2.setOnClickListener(new OnClickListener() {
// final boolean val= getIntent().getExtras().getBoolean("tg");
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i1=new Intent(Calculator_3Activity.this ,Calculator_4Activity.class);
startActivity(i1);
//finish();
}
});
回答1:
As Calculator1.java has
intent.putExtra("toggleBtn", tg.isChecked());
in Calculator2.java
final boolean val= getIntent().getExtras().getBoolean("toggleBtn");
instead of final boolean val= getIntent().getExtras().getBoolean("tg");
回答2:
Calculator1.java
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Calculator_1Activity.this, Calculator2.class);
intent.putExtra("toggleBtn", tg.isChecked());
startActivity(intent);
}
});
Calculator3.java
final boolean val= getIntent().getExtras().getBoolean(**"toggleBtn"**);
来源:https://stackoverflow.com/questions/14705964/intent-not-working-properly-in-switch-between-different-activities