-2

I am making a program in which i need to pass a value from one activity to another using an Intent between activities.

So here is my question How to Pass and Get an Intent into another activity.

See my below code, using button click i want to pass an intent to another activity..

FirstActivity.java:

 public class FirstActivity extends Activity {
// Initializing variables
EditText inputName;
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);

    inputName = (EditText) findViewById(R.id.name);
    Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);

    //Listening to button event
    btnNextScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Code to start a new Intent and to pass value


        }
    });
}
JellyBean
  • 60
  • 4

5 Answers5

5

Nothing hard here, you just need to pass value of name from FirstActivity to SecondActivity by using below code:

    btnNextScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            //Starting a new Intent
            Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);

            //Sending data to another Activity
            nextScreen.putExtra("name", inputName.getText().toString());
            startActivity(nextScreen);

        }
    });
}

and in second activity use below code in onCreate() method:

 TextView txtName = (TextView) findViewById(R.id.txtName);     
 Intent i = getIntent();
    // Receiving the Data

    String name = i.getStringExtra("name");
    txtName.setText(name);
Android
  • 1,529
  • 1
  • 12
  • 27
  • 1
    http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context. getApplicationContext() is not recommended – Raghunandan Apr 17 '13 at 05:41
1

yes Ketlyen i agree with @klamitsuri

You just need to pass value like he shown in code:

  Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);

        //Sending data to another Activity
        nextScreen.putExtra("name", inputName.getText().toString());
        startActivity(nextScreen);

and to get simply use:

  Intent i = getIntent();
// Receiving the Data

String name = i.getStringExtra("name");
Sun
  • 6,768
  • 25
  • 76
  • 131
0

In your firstActivity

 btnNextScreen.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
       Intent i= new Intent(firstActivity.this,secondActivity.class);
       i.putExtra("key","mystring");// key and the value
       startActivity(i);

    }
  });
}

In your second activity onCreate()

setContentView(R.layout.second); 
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
 TextView tv= (TextView)findviewById(R.id.textView1);
 String value = extras.getString("key"); // get the value based on the key
 tv.setText(value); 
} 
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

In your current Activity, create a new Intent:

Intent i = new Intent(FirstActivity.this, NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}
rajeshwaran
  • 1,512
  • 1
  • 18
  • 24
0

You can pass the values or data by using startActivityForResult(intent,result);

and to get the values sent by the previous activity override the function onActivityResult(int requestCode, int resultCode,Intent data);

for Example:

 public class MyActivity extends Activity {
 ...

 static final int PICK_CONTACT_REQUEST = 0;

 protected boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
         // When the user center presses, let them pick a contact.
         startActivityForResult(
             new Intent(Intent.ACTION_PICK,
             new Uri("content://contacts")),
             PICK_CONTACT_REQUEST);
        return true;
     }
     return false;
 }

 protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == PICK_CONTACT_REQUEST) {
         if (resultCode == RESULT_OK) {
             // A contact was picked.  Here we will just display it
             // to the user.
             startActivity(new Intent(Intent.ACTION_VIEW, data));
         }
     }
 }}
Aayush Rana
  • 1,303
  • 1
  • 12
  • 19