0

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?

Rain Man
  • 1,163
  • 2
  • 16
  • 49

2 Answers2

0

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:

Community
  • 1
  • 1
0

Instead of getting the context from the View you should get the Activity Context. Try instead of this:

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

that:

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

actually when your in an activity you could also just do startActivity(intent);

safari
  • 7,565
  • 18
  • 56
  • 82