3

enter image description hereI can send data from the first activity but on repeating the same procedure on the second activity to send data to ble device is not successful. How can I send data from second activity?

sitara
  • 61
  • 11
  • you want to send data from second activity to first or from second to third activity? – Anjali Nov 17 '15 at 08:13
  • I want to send data to ble device – sitara Nov 18 '15 at 09:27
  • I declare the bluetooth functions in the first activity. I did the same in the second activity to send data to ble device from the second page of my app like the first page do. But it didn't work. Only from the first page data send to ble. – sitara Nov 18 '15 at 09:55

2 Answers2

0

use this to save

Intent intent = new Intent(FirstScreen.this, SecondScreen.class)
    intent .putExtra(strName, keyIdentifer );

use this to get

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}
Kastriot Dreshaj
  • 1,121
  • 6
  • 16
0

If you just want to send data to the next activity, use Intent intent = new Intent(FirstActivity.this, SecondActivity.class) intent.putExtra("id_for_value", value); startActivity(intent);

And recover it with

  value= getIntent().getExtras().getString("id_for_value");//if it is a string

OR

If you want to send data back from the Second activity back to the Previous, you have to use start activity for results

Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
startActivityForResult(intent, 2)//where 2 is the request code
finish();

Again in the FirstActivity, overide this

@Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data)  
   {  
             super.onActivityResult(requestCode, resultCode, data);  
              // check if the request code is same as what is passed  here it is 2  
               if(requestCode==2)  
                     {  
                        String result=data.getStringExtra("ResultId");   

                     }  
 }  

And in your PreviousActivity, you pass the data like this

Intent intent=new Intent();  
intent.putExtra("ResultId",message);  
setResult(2,intent);  
finish();
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43