Pass int to another Activity

Before you flame me: I know there are uncountable tutorials out there, and I know myself how to pass data to another ACtivity, just like that.

In my case that’s diffrent tho. “Usually” data is passed to another activity through Intents, Bundles ecc and the other Activity is started.

Here’s my case:

I have an Item with 4 parameters (Image, String,String, int) In an AdapterClass I have a PopUpView which retakes those 4 parameters.

What I’d like to achieve is the following: With the click of a button, the 4th parameter, the int should be sent to the Main activity and inserted in a textView inside the MainActivity, without (here’s the main diffrence between this and the other questions)launching the Main Activity.

How can this be done?

TIA.

Answer

use BroadcastReceiver to send that 4th int to MainActivity in PopupView do these:

    Intent intent = new Intent("SOMEACTION");
    intent.putExtra("4th_int", value);
    activity.sendBroadcast(intent);'

//In MainActivity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        IntentFilter filter = new IntentFilter("SOMEACTION");
        this.registerReceiver(mReceiver , filter); 
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            this.unregisterReceiver(this.mReceiver );
        }

        private BroadcastReceiver mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction() == "SOMEACTION") {
                     // retrieve the 4th int value and update something in MainActivity
                }
            }
        };

Leave a Reply

Your email address will not be published. Required fields are marked *