Reputation: 15525
I have onClick
method in MainActivity
as following,
public void onClick(View view) {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I", "0");
startActivity(i);
}
In SecondActivity
I validated the Intent
as following
if (getIntent().getStringExtra("I") == null) {
Toast.makeText(this, "NULL", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "NOT NULL", Toast.LENGTH_LONG).show();
}
and started the ThirdActivity
from SecondActivity
as following
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivity(i);
}
Now when come back to SecondActivity
from ThirdActivity
getIntent().getStringExtra("I")
returns null
. I also tried overriding onOptionsItemSelected
as following.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = getIntent();
i.putExtra("I", "0");
break;
}
return super.onOptionsItemSelected(item);
}
Now don't know how to fix this issue. I don't want to use SharedPreference
for this issue.
Upvotes: 0
Views: 2588
Reputation: 1621
You are not finishing any activity, and you are passing value from main activity to second, then calling third activity, just returning from the third activity and resuming the second activity, you are not passing any data to second activity while coming from third activity.
just change your code as follow:-
//from second to third
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivity(i);
finish();
}
// returning from third to second
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = new Intent(this, second.class);
i.putextra("I","0");
startActivity(i);
finish();
break;
}
return super.onOptionsItemSelected(item);
}
also you can change like this:-
@Override
public void onBackPressed() {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I","0");
startActivity(i);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
in your third activity only
if you dont want to startactivty then startActivtyForResult() from second to third:-
startActivityForResult(i,101);
implement onActivtyResult method in second activity:-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 101 && resultCode == RESULT_OK) {
// Now use data.getStringExtra("I");
}
}
for third activity just finish it in onBackpress() like:-
@Override
public void onBackPressed() {
Intent resultData = new Intent();
resultData.putExtra("I", "0");
setResult(Activity.RESULT_OK, resultData);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 1
Reputation: 10126
Please follow these steps:
start ThirdActivity for result
public void onClick(View view) {
Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);
}
Update following method, add setResult(Activity.RESULT_OK, i);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = getIntent();
i.putExtra("I", "0");
setResult(Activity.RESULT_OK, i);
break;
}
return super.onOptionsItemSelected(item);
}
In SecondActivity, implement onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1 && resultCode == RESULT_OK) {
// Now use data.getStringExtra("I");
}
}
Upvotes: 2
Reputation: 2164
You can try using startActivityForResult()
method for your linking from SecondActivity
to ThirdActivity
this way, you can then use setResult(intent)
before killing the ThirdActivty
. Thus when you come back to your SecondActivity
you will have the result intent you passed in your ThirdActivty
inside the onActivityResult
method of your SecondActivity
.
Hope this helps.
Upvotes: 1
Reputation: 283
try this : put this on your thirdactivity
@Override
public void onBackPressed() {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("I","0");
startActivity(i);
finish();
}
Upvotes: 0
Reputation: 359
you have to pass again
i.putExtra("I", "0");
from third activity to second activity
Upvotes: 0
Reputation: 790
Make a change when you move towards SecondActivity from ThirdActivity.:
Intent i = new Intent(this, SeconActivity.class);
i.putExtra("I",getIntent().getStringExtra("I"));
startActivity(i);
Upvotes: 0
Reputation:
This is because when you come back from third activity to second activity, getIntent() returns the Intent of third activity and not the first.
And while navigating from third activity to second, you are not passing any string extra and hence you get a null value.
Upvotes: 1