Intent not working properly in switch between different activities

回眸只為那壹抹淺笑 提交于 2019-12-25 04:09:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!