I have two activity, and when I press enter on first activity, it will open second activity, it contains a ListView
and when I choose an item from ListView
, it will get its value and bring back to first activity
this is what I've tried;
on second activity
listPerasat.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
NamaPrst = ((TextView) view.findViewById(R.id.txtListNamaPrst)).getText().toString();
Intent i = new Intent();
i.putExtra("NAMA_PERASAT", NamaPrst);
finish();
}
});
}
on first activity
edtText.setText(getIntent().getStringExtra("NAMA_PERASAT")); // inside onCreate
or
public void onActivityResult(int requestCode,int resultCode, Intent data)
{
edtText.setText(getIntent().getStringExtra("NAMA_PERASAT"));
}
but nothing happen. how can I get intent and back to previous activity?
You forgot to start an activity so right before
i.e.
Refer to this link : http://developer.android.com/training/basics/intents/result.html
Hope it helps you.
In
second activity
edit your code like thisthen in
first activity's
onActivityResult(), first check if the result was "OK" i.e. something was selected.If after opening second activity you didn't selected anything and pressed
back button
then you will get errors since nothing was passed fromsecond activity
. So first check that if thesetResult(RESULT_OK, i);
was executed i.e.extras
were added and we manually set that everything isOK
now.Try this:
call the second activity for result. it will return your selected data on OnAcitiviyResult. just like below:
link to post
You want to call
startActivityForResult()
for this. So in your firstActivity
something likethen in your
onItemClick()
you need to callsetResult()
and send back theIntent
. This will callonActivityResult()
in your firstActivity
In
onActivityResult()
in your firstActivity
, don't callgetIntent()
. This will try to use theIntent
that originally started your firstActivity
. Instead, use theIntent
you passed backSee this answer for another example