I have 3 activities
1st activity calls 2nd activity and the 2nd activity calls the 3rd activity.
2nd activity uses intent extras from the 1st activity.
So when I return (using actionbar back button) to 2nd activity from the 3rd activity, I get a nullpointerexception on the place where i extract the intent extra. Note: this does not happen if I press the navigationbar back button).
On pressing the action bar back button, it is restarting the activity, hence no intent.
On pressing navigation bar back button, it is resuming the fragment and hence I am able to display my parceable data.
Any clues on how to save the intent extras? One workaround that I thought was saving it as sharedpreferences, but I want to know the best practice for doing this.
Edit: added code
From fragment of activity 1.
Intent intent = new Intent(getActivity(), TopTenTracksActivity.class)
.putExtra(Intent.EXTRA_TEXT, new String[]{artistId, artistName});
startActivity(intent);
Getting intent in fragment of activity 2
String[] artistInfo = getActivity().getIntent().getExtras().getStringArray(Intent.EXTRA_TEXT);
Get the null pointer error on the above.
Error log
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
at com.plusgaurav.spotifystreamer.TopTenTracksActivityFragment.onCreateView(TopTenTracksActivityFragment.java:62)
EDIT: This is the following code that I have:
public class TopTenTracksActivity extends AppCompatActivity {
public static String[] artistInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
artistInfo = savedInstanceState.getStringArray("savedArtistInfo");
} else {
// get intent info
artistInfo = getIntent().getExtras().getStringArray(Intent.EXTRA_TEXT);
}
setContentView(R.layout.activity_top_ten_tracks);
// set subtitle in the actionbar
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setSubtitle(artistInfo[1]);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save intent
outState.putStringArray("savedArtistInfo", artistInfo);
}
It is still going to the else loop mentioned above. My question is why is the savedInstanceState null when I press ActionBar back.
The ActionBar
back is really the "Up" button. The distinction between it and the device back button is that the "Up" button is meant to go up in the screen hierarchy (back to an instance of the parent activity) while the device back button is meant to go back chronologically (literally the next activity on the back stack).
Depending on how you start your activity and the launch mode you specified for that activity, when you hit the "Up" button you may be directed to a new instance of that parent activity rather than an existing instance.
Please read the following for more details: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp
My guess is that what is happening is this (from the page I linked):
If the parent activity has launch mode <standard>, and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP, the parent activity is popped off the stack, and a new instance of that activity is created on top of the stack to receive the intent.
Try setting the launch mode of your activity 2 (or whichever activity for which you appear to be losing extras) to singleTop
.