0

In my android application suppose several activities are there if using intent I go to other activities like this.

If user choose correct answer on Activity A, it will bring to Activity C

[Activity A]->[activity C]

else, user choose the wrong answer in activity A, it will bring the user to activity B before going to Activity C

[Activity A]->[activity B]->[Activity C]

Activity C acts to show the result.

However, when I write code in Activity C to appear result. The result did not show bring me back to Activity A again.

    TextView text = (TextView) findViewById(R.id.Solution);
    ImageView imagee = (ImageView) findViewById(R.id.solutionImage);
    String cconection = getIntent().getStringExtra("SpinConnection");//get from Activity B
    String ccondition = getIntent().getStringExtra("SpinCondition");//Get from Activity B
    String txtPowerSupply = getIntent().getStringExtra("PowerCable"); //get from Activity A

    if (txtPowerSupply.equals("PowerOff")) {

        text.setText("A+");
        imagee.setImageResource(R.drawable.sakura);
    }

    if (cconection.equals("Not properly connected") && ccondition.equals("Good")) {
    text.setText("B+");
    imagee.setImageResource(R.drawable.sakura); }

Activity A

               switch(view.getId()) {
        case R.id.PowerOn:
            if (checked)
            {
                Intent monpage = new Intent(ActivityA.this, com.example.lenovo.computerhardwarediagnostic.ActivityB.class);
                startActivity(monpage);
            }
            break;
        case R.id.PowerOff:
            if (checked) {
                //Intent intent = new Intent(getApplicationContext(), Solution.class);
                //int genderID = powerSupply.getCheckedRadioButtonId();
                Intent data = new Intent(ActivityA.this, com.example.lenovo.computerhardwarediagnostic.ActivityC.class);
                data.putExtra("PowerCable","PowerOff");
                startActivity(data);
               finish();
            }
            break;
        }

Activity B

            if(fact1.equalsIgnoreCase("Not properly connected")&&fact2.equalsIgnoreCase("Good"))
            {
                Intent intent = new Intent(ActivityB.this, ActivityC.class);
                intent.putExtra("SpinConnection","Not properly connected");
                intent.putExtra("SpinCondition","Good");
                startActivity(intent);

            }

I'm a beginner in Android. Can you help me to solve this?

Mittal Patel
  • 2,732
  • 14
  • 23
  • Show your code that does `intent.putExtra` please https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application?rq=1 – OneCricketeer Sep 20 '18 at 16:40

1 Answers1

0

The problem is that you can only retrieve the intent that initiated ActivityC, so the information stored in the intent sent by ActivityA is lost.

In ActivityB extract the information from ActivityA and pass it to ActivityC.

Try this:


In Activity A

               switch(view.getId()) {
        case R.id.PowerOn:
            if (checked)
            {
                Intent monpage = new Intent(ActivityA.this, com.example.lenovo.computerhardwarediagnostic.ActivityB.class);

                //Pass the info to ActivityB
                monpage.putExtra("PowerCable","PowerOn");

                startActivity(monpage);
            }
            break;
        case R.id.PowerOff:
            if (checked) {
                //Intent intent = new Intent(getApplicationContext(), Solution.class);
                //int genderID = powerSupply.getCheckedRadioButtonId();
                Intent data = new Intent(ActivityA.this, com.example.lenovo.computerhardwarediagnostic.ActivityC.class);
                data.putExtra("PowerCable","PowerOff");
                startActivity(data);
               finish();
            }
            break;
        }

Activity B

if(fact1.equalsIgnoreCase("Not properly connected")&&fact2.equalsIgnoreCase("Good"))
            {
                //getting info from ActivityA
                String powerCableValue = getIntent().getStringExtra("PowerCable");

                Intent intent = new Intent(ActivityB.this, ActivityC.class);
                //passing info to ActivityC
                intent.putExtra("PowerCable",powerCableValue);

                intent.putExtra("SpinConnection","Not properly connected");
                intent.putExtra("SpinCondition","Good");
                startActivity(intent);

            }
aaroncio
  • 307
  • 1
  • 6
  • I didn't see that you have two different intents, try again adding line `monpage.putExtra("PowerCable","PowerOn");` I edited the post with this update. – aaroncio Sep 20 '18 at 18:52