Rain Man Rain Man - 23 days ago 7
Android Question

Working with intent data when dealing with several activities

I have several activities which are are linked together. There are :

Activity_1 -> Activity_2 -> Activity_3
where activity 1 is the parent of activity 2 and activity 2 is parent of activity 3. The manifest is as follow:

<activity android:name=".Activity_1"
...>
<activity android:name=".Activity_2">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.Activity_1" />
</activity>
<activity android:name=".Activity_3">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activities.Activity_2" />

</activity>


in
Activity_1
adapter I send some data to
Activity_2
via onclick listener (through the adapter) :

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(v.getContext(), Activity_2.class);
intent.putExtra("title",title.getText());
v.getContext().startActivity(intent);
}
});


and in
Activity_2
I retrieve that data:

....
private String title;
....
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
title = extras.getString("title");
}
...
// some important usage of title here


I have another onclick listener that goes from activity 2 to activity 3 and when I click on the back button, the app crashes because title is returned
null
.

I dont want to use preferences to store the title, any idea how to avoid this?

Answer

It seems like you are fetching info from the intent during onResume which is called from Activity1 -> Activity2 and also in the way back from Activity3 -> Activity2.

You may want to consider doing so during onCreate instead. Does that make sense?

EDIT: So... I just learnt that navigation up (the "<-" on the action bar) is different from navigation back (hard button). You can solve this issue using different approaches: