Open top menu


 How to pass data from one activity to another in android ?

 Android user interface is show through an activity. Activity is used to represent the  user information and allows user interaction.  During activity interaction we might required to transfer data from one activity to other activity. i have explain in briefly-  

* how to activity interact via intent ? explain
   pass the value via intent object,
   intent.putExtera("key name","your value");
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  String message=" Help me";
  
  Intent intent=new Intent(MainActivity.this,AnotherActivity.class);
  
  //intent.putExtra(< put your key name>, <your passing value>);
  
  intent.putExtra("data", message);
  startActivity(intent);
  
  
 }
 

}
you can retrieve the value via getIntent(); method on intent object;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class AnotherActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.anotheracitvity);
       //get string value
  Intent intent=getIntent();
  String message= intent.getStringExtra("data");
  
  
  
 }

}

Tagged

0 comments