Kazhiunea
Kazhiunea

Reputation: 11

How to check if a button is pressed in another activity?

In my first Activity I made that you can open a pop-up window (it's another activity), where you can put some data in and it needs to be sent back to the original Activity. Since I will need to put the data I got into a ListView, I think I need to check if the button is pressend and in the onClick add to the ListView. How to do that?

Here's the code of the pop-up Activity:

public class AddCoin extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        final Intent intentAdd = new Intent(this, PortfolioActivity.class);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.add_coin_window);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width = dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int)(width*.8), (int)(height*0.4));

        Button add_coin = findViewById(R.id.add_toport_button);
        add_coin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText inputNameText = (EditText) findViewById(R.id.text_input_name);
                EditText inputPriceText = (EditText) findViewById(R.id.text_input_price);
                EditText inputAmmountText = (EditText) findViewById(R.id.text_input_ammount);

                String inputName = inputNameText.getText().toString();
                String inputPrice = inputPriceText.getText().toString();
                String inputAmmount = inputAmmountText.getText().toString();

                intentAdd.putExtra("Name", inputName);
                intentAdd.putExtra("Price", inputPrice);
                intentAdd.putExtra("Ammount", inputAmmount);

                finish();
            }
        });
    }
}

Upvotes: 1

Views: 70

Answers (3)

Bakhtiyor Begmatov
Bakhtiyor Begmatov

Reputation: 483

Proper way is calling activity with startActivityForResult().

But if you can not do this you can use EventBus

Upvotes: 1

E.Abdel
E.Abdel

Reputation: 1992

You have to use startActivityForResult(), put data into an Intent on called activity and then get results back using onActivityResult(int requestCode, int resultCode, Intent data) on the calling activity

Upvotes: 0

buzzingsilently
buzzingsilently

Reputation: 1586

You can achieve this nature using startActivityForResult() method of Activity class.

Refer this link startActivityForResult

Upvotes: 0

Related Questions