Reputation: 15
In an activity I have 2 buttons that those get their text from database. I have a problem with them, at first I set first button text and then click other button and go to another activity to choose my text from database but when I come back to previous activity first button text is null. I try it with Intent and pass it to another activity and then retrieve it but it was not helpful!
What is your opinion if you test it before?
best regards
btnIncomeFrom= (Button) findViewById(R.id.btnDaryaftAz);
btnIncomeFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(NewIncomeActivity.this, IncomeListActivity.class));
}
});
IncomeTitle = getIntent().getStringExtra("ThirdLevelIncome");
btnIncomeFrom.setText(IncomeTitle);
btnSettleTo= (Button) findViewById(R.id.btnVarizBe);
btnSettleTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NewIncomeActivity.this, SettleToListActivity.class);
//intent.putExtra("IncomeTitle",IncomeTitle);
startActivity(intent);
}
});
btnSettleTo.setText(getIntent().getStringExtra("ThirdLevelSettle"));
Upvotes: 0
Views: 78
Reputation: 15
I solved my problem just with one condition:
if(getIntent().getIntExtra("temp",0)== 1)
{
btnIncomeFrom.setText(getIntent().getStringExtra("FinalIncomeTitle"));
}
else
{
IncomeTitle = getIntent().getStringExtra("ThirdLevelIncome");
btnIncomeFrom.setText(IncomeTitle);
}
btnSettleTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NewIncomeActivity.this, SettleToListActivity.class);
intent.putExtra("IncomeTitle",IncomeTitle);
startActivity(intent);
}
});
I passed temp with value 1 and FinalIncomeTitle that I passed to second activity when I want to come back to previous activity
intent.putExtra("FinalIncomeTitle",getIntent().getStringExtra("IncomeTitle"));
intent.putExtra("temp",1);
Upvotes: 0
Reputation: 121
In my opinion better way to achieve this is use startActivityForResult() method. For example:
final int JADID_ACTIVITY_RESCODE = 1;
@Override
public void onClick(View view) {
startActivityForResult(new Intent(DaryafteJadidActivity.this, IncomeListActivity.class),JADID_ACTIVITY_RESCODE);
}
/* invoked when started activity returns */
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == JADID_ACTIVITY_RESCODE) {
if (resultCode == RESULT_OK) {
String retrived_data = data.getStringExtra("STRING_NAME","");
}
}
}
And in Activity, after you retrieve data you can do:
Intent resultIntent = new Intent();
resultIntent.putExtra("STRING_NAME",retrived_value);
setResult(RESULT_OK,resultIntent );
finish();
Upvotes: 1