I have an activity that was passed a value in an intent. I'll call it activity A. Activity A's job is to show a list. From A, I added a menu option to start activity B, whose job it is to hold the form to fill out a new list item. I use an intent to create activity B, which works just fine.
My issue is, when the user navigates back to activity A, I lose the value that was initially passed in via my intent.
[EDIT]: As I've come to learn, I didn't mean back in the previous sentence, I meant UP. See bottom of my post for more.
In activity A, I tried putting the following code, though savedinstanceState is null in onCreate when I navigate back from activity B.
To create activity B, I tried startActivity and startActivityForResult, and in activity B, I use the default back button on the top left to close the activity.
[EDIT]: As I've come to learn, I didn't mean back in the previous sentence, I meant UP. See bottom of my post for more.
How do I store the value of item ID in activity A when I need to come back from Activity B?
EDIT: Full code of Activity A:
public class ActivitiesActivity extends ActionBarActivity {
public long item_id;
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putLong(Global.itemIdKey, item_id);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
item_id = savedInstanceState.getLong(Global.itemIdKey);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
item_id = savedInstanceState.getLong(Global.itemIdKey);
}
else {
item_id = getIntent().getLongExtra(Global.itemIdKey, item_id);
}
setContentView(R.layout.activity_activities);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActivitiesActivityFragment frag =
(ActivitiesActivityFragment) getFragmentManager().findFragmentById(R.id.activitiesFragment);
frag.setSkillId(item_id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activities, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_addactivity)
{
Intent intent = new Intent(this, ActivityAdd.class);
startActivityForResult(intent, 0);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Full code of activity B - I use a fragment here that's empty. Right now I'm just using the native back button to navigate back up to activity A, but I've also tried calling finish() explicitly from a button.
public class ActivityAdd extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_add);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
EDIT: Thanks for the answers below. Initially, I implemented shared preferences, though when reading further, I mistakenly said I was navigating BACK from activity B, which this was not the case - I was navigating UP from activity B, which behaves differently from BACK. UP was creating a new instance of Activity A rather than returning to the existing instance until I added
android:launchMode="singleTop"
in my activity declaration in my manifest. I found this in this answer.