1

Here i wrote a code to pass data from one activity to another activity by using intents.. Please let me know if i need to do any corrections over here in my code.

   OnClickListener buttonListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent nextIntent = new Intent(getApplicationContext(), SecondActivity.class);
        nextIntent.putExtra("firstname", "Siva");
        nextIntent.putExtra("Secondname", "Kumar");
        startActivity(nextIntent);


        Toast.makeText(getApplicationContext(),"SignIn Button Clicked", Toast.LENGTH_SHORT).show();
    }
};

Second Activity:

OnClickListener backListener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent backIntent = new Intent(getApplicationContext(), MainActivity.class);
        Intent receivedIntent = getIntent();
        Bundle bundleData = receivedIntent.getExtras();
        bundleData.getString("firstname");
        bundleData.getString("secondname");
        startActivity(backIntent);
    }
};

2 Answers2

1

You should try like this

  1. pass data to SecondActivity

    Intent nextIntent = new Intent(getApplicationContext(), SecondActivity.class);
    nextIntent.putExtra("firstname", "Siva");
    nextIntent.putExtra("Secondname", "Kumar");
    startActivity(nextIntent);
    
  2. get data inside SecondActivity

    Intent receivedIntent = getIntent();
    String firstname=receivedIntent.getStringExtra("firstname");
    String secondname=receivedIntent.getStringExtra("Secondname");
    

EDIT

SecondActivity.jaja

public class SecondActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    Intent receivedIntent = getIntent();
    String firstname=receivedIntent.getStringExtra("firstname");
    String secondname=receivedIntent.getStringExtra("Secondname");

    TextView txtFirst=(TextView)findViewById(R.id.text1);
    txtFirst.setText(firstname);
    TextView txtSecond=(TextView)findViewById(R.id.text2);
    txtSecond.setText(secondname);
    Button back=(Button)findViewById(R.id.backbutton);
    back.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        finish();


        }
    });

}

}
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45
0

Use some String variables like below,

OnClickListener backListener = new OnClickListener() {

    @Override
    public void onClick(View v) {

        Intent backIntent = new Intent(getApplicationContext(), MainActivity.class);
        Intent receivedIntent = getIntent();
        String firstName = getIntent().getStringExtra("firstname");
        String secondName = getIntent().getStringExtra("Secondname");
        startActivity(backIntent);
    }
};

Now your two String variables firstName and secondName contains the information you required.

Vigbyor
  • 2,568
  • 4
  • 21
  • 35