Skip to content Skip to sidebar Skip to footer

Using Intent To Send Data

How can i use intent to send data such as a string from activity A to activity B without leaving activity A? I also need to know how to capture the data in activity B and add it to

Solution 1:

what you are looking for is Brodcast Reciver:

activity A should send brodcast:

publicclassActivityAextendsActivity
{
     privatevoidsendStringToActivityB()
     {
         //Make sure to have started ActivityB first, otherwise B wont be listening on the receiver:
         startActivity(ActivityA.this, ActivityB.class);
         //Then send the data
         Intent intent = new Intent("someIntentFilterName");
         intent.putExtra("someKeyName", "someValue");
         sendBroadcast(intent);
     }
}

and activity B should implement receiver:

publicclassActivityBextendsActivity
    {
        private TextView mTextView;

        privateBroadcastReceivermBroadcastReceiver=newBroadcastReceiver()
        {       
            @OverridepublicvoidonReceive(Context context, Intent intent)
            {
                StringstrValueRecived= intent.getStringExtra("someKeyName","defaultValue");
                mTextView.setText(strValueRecived);
            }
         };

         @OverrideprotectedvoidonCreate(Bundle savedInstanceState)
         {
              super.onCreate(savedInstanceState);
              mTextView = (TextView)findViewById(R.id.textView); 


              registerReceiver(mBroadcastReceiver, newIntentFilter("someIntentFilterName"));
         } 
} 

the example not complete, but you can read about it on the link: http://developer.android.com/reference/android/content/BroadcastReceiver.html

Post a Comment for "Using Intent To Send Data"