how to get values from previous activity
hi all in second activity i set some values and how to bring into first activity. i use Bundle for bring data. NullPointerException error is getting.
in secondact开发者_运维技巧ivity:
c1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
c1.setChecked(true);
Bundle bundle = new Bundle();
bundle.putString("key1",String.valueOf(c1.getText()));
Intent myIntent = new Intent(view.getContext(), main.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
}
});
in first(main) activity:
Bundle bundle = this.getIntent().getExtras();
check = bundle.getString("key1"); // NullPointerException.
please assist me.
It looks like you are launching your secondactivity
from your firstactivity
(main). In this case, you need to return the data using finish()
and not again start the first activity using startActivityForResult()
.
In your secondactivity
, use:
Bundle bundle = new Bundle();
bundle.putString("key1",String.valueOf(c1.getText()));
Intent myIntent = new Intent(view.getContext(), main.class);
myIntent.putExtras(bundle);
setResult(RESULT_OK, myIntent);
finish();
And in the onActivityResult()
of firstactivity
, use:
@Override
public void onActivityResult(int reqCode, int resCode, Intent data)
{
Bundle bundle = data.getExtras();
check = bundle != null ? bundle.getString("key1") : "";
}
You have to Start Activity For Result mechanism. So when the second activity is done to return the result to the first activity.
Mojo Risin is right you have to use Start Activity For Result mechanism.
You can see complete working tutorial here http://www.vogella.de/articles/AndroidIntent/article.html#explicitintents
Intent myIntent=new Intent(v.getContext(),detailPage.class);
myIntent.putExtra("prodId", "data");
startActivity(myIntent);
to fetch
String index=""
index=getIntent().getExtras().getString("prodId");
System.out.println("Position :"+index);
精彩评论