-4

I've searched far and wide in stackoverflow but i couldnt find the answer. What would be the appropriate code for updating settext on MULTIPLE buttons when the intent string is pulled from another activity?

My mainActivity contains three buttons containing three different currencies by country. If a user clicks on one of the buttons in the mainActivity, they will go to the secondActivity where they choose another country. Once it is selected, they will go back to the mainActivity and the country that was selected should replace the default text on the button.

What happens when i do the code below is everytime i choose an option in secondActivity, all the buttons text will change the the option selected. It should only be the button that was clicked.

//mainActivity class
    region1 = (Button)findViewById(R.id.region1);
    region2 = (Button)findViewById(R.id.region2);
    region3 = (Button)findViewById(R.id.region3);

    region1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, List.class);
            startActivity(i);
        }
    });

    region2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, List.class);
            startActivity(i);
        }
    });

    region3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, List.class);
            startActivity(i);
        }
    });

    region1.setText(getIntent().getStringExtra("btn"));
    region2.setText(getIntent().getStringExtra("btn"));
    region3.setText(getIntent().getStringExtra("btn"));

//secondActivity class
        lst_eur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(List.this, MainActivity.class);
                i.putExtra("btn", "EUR");
                startActivity(i);
            }
        });

        lst_gbp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(List.this, MainActivity.class);
                i.putExtra("btn", "GBP");
                startActivity(i);
            }
        });

        lst_jpy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(List.this, MainActivity.class);
                i.putExtra("btn", "JPY");
                startActivity(i);
            }
        });
  1. In mainActivity, I click region1
  2. Then it takes me to secondActivity
  3. I click lst_eur
  4. It takes me back to mainActivity with region1 updated from lst_eur intent

region2 and region3 should not be changed and will only be changed if it goes through similar process above.

ADM
  • 20,406
  • 11
  • 52
  • 83
Cattarina
  • 61
  • 1
  • 2
  • 10
  • Make use of [`startActivityForResult`](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android). – ADM Jan 26 '19 at 15:09
  • Yes, but this doesn't explain how to incorporate this in multiple buttons. Whenever i get data from second activity, it changes all the buttons to similar texts. – Cattarina Jan 26 '19 at 16:50

2 Answers2

0

You can simply use activity onPause() and onResume() methods for update text in multiple activities or go back from activity. Write your code in these two methods for updation.

You can also use startActivityForResult for this.

Jai kumar
  • 45
  • 7
0

replace to this one:

if(getIntent().getStringExtra("btn").equels("EUR")){
 region1.setText(getIntent().getStringExtra("btn"));
 region2.setText("Default text");
 region3.setText("Default text");
}else if(getIntent().getStringExtra("btn").equels("GBP")){
 region1.setText("Default text");
 region2.setText(getIntent().getStringExtra("btn"));
 region3.setText("Default text");
}else if(getIntent().getStringExtra("btn").equels("JPY")){
 region1.setText("Default text");
 region2.setText("Default text");
 region3.setText(getIntent().getStringExtra("btn"));
}