This is a Java language project for Android application using Android Studio.
I go from Activity A
to Activity B
and then back to Activity A
. Nothing is transferred from Activity A
to Activity B
, but on the way from Activity B
to Activity A
there is. Here is code taking you from Acitvity B
to Activity A
:
@Override
public void onClick(View v){
Intent main_intent = new Intent(PeriodicTableActivity.this, MainActivity.class);
main_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main_intent.putExtra("Element", String.valueOf(v.getTag()));
startActivity(main_intent);
overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
}
On the way back from Activity B to Activity A, I want to use Bundle savedInstanceState
to restore a HashMap<Integer, ElementInfo>
instead of parsing web URL
that fills the HashMap
in the opening of the Android application.
if (savedInstanceState != null)
elementInfoHashMap = (HashMap<Integer, ElementInfo>) savedInstanceState.getSerializable("ELEMENTS");
try {
InputStream input1 = getAssets().open("periodic_table.csv");
elementInfoHashMap.putAll(pullElementURLData());
} catch (IOException | NetworkOnMainThreadException | ExecutionException | InterruptedException e) {
e.printStackTrace();
}
Here is how I store my HashMap<Integer, ElementInfo>
when I leave Activity A
.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putSerializable("ELEMENTS", elementInfoHashMap);
}
My issue is that whenever I return to Activity A
from Activity B
, my savedInstanceState
is null
. Does anyone have an clue why savedInstancedState
is null
when returning from Activity B
to Activity A
?