Skip to content Skip to sidebar Skip to footer

Extra From Activity B To Activity A

I have two activities. In Activity A I have Textview. After click this I go to Activity B with EditText. How can I pass the value from the EditText in activity B back to the TextV

Solution 1:

Write Activity A like this

publicclassMainActivityextendsActivity {  
TextView textView1;  
Button button1;  
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    textView1=(TextView)findViewById(R.id.textView1);  
    button1=(Button)findViewById(R.id.button1);  
    button1.setOnClickListener(newOnClickListener() {  
        @OverridepublicvoidonClick(View arg0) {  
            Intent intent=newIntent(MainActivity.this,SecondActivity.class);  
            startActivityForResult(intent, 2);// Activity is started with requestCode 2  
        }  
    });  
}  
// Call Back method  to get the Message form other Activity  @OverrideprotectedvoidonActivityResult(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 message=data.getStringExtra("MESSAGE");   
                        textView1.setText(message);  
                     }  
 }  
 }

Insted of startActivity use startActivityForResult

Intent intent=newIntent(MainActivity.this,SecondActivity.class);  
        startActivityForResult(intent, 2);// Activity is started with requestCode 2  

and in Activity B set result like this

   Intent intent=newIntent();  
                intent.putExtra("MESSAGE",message);  
                setResult(2,intent);  
                finish();//finishing activity  

you can get intent.putExtra("MESSAGE",message); in Activity A(onActivityResult)

Post a Comment for "Extra From Activity B To Activity A"