0

I have a ListView and each of the elements of the list has its own detail view. Each of the detail views also has its "detail view". When an item in the list is clicked, I send the id of the item to the first detail view (using an Intent) and I use that to fetch the data. However, when I go to the second detail view and navigate back, the Activity throws a NullPointerException, because (of course) the getIntent().getExtras() is empty now.

Is there a way to solve this problem without having to change my approach? And if not what should I do to solve this? Thanks.

Loolooii
  • 8,588
  • 14
  • 66
  • 90

1 Answers1

1

One approach is to send those extras to the detail view, and then when you navigate back, you send those extras back, so when your activity (the one with the listView) it won't crash.

It would look something like:

baseActivity.java (this is the one with the ListView)

Intent intent = new Intent(this, detailView.class);
intent.putExtra("nameHere1", valueHere1);
intent.putExtra("nameHere2", valueHere2);
//just put extra's here
startActivity(intent);
finish();

and in your detailView.java would do something like this in your onBackPressed:

Intent intent = new Intent(this, baseActivity.class);
intent.putExtra("nameHere1", getIntent().getExtra("nameHere1"));
intent.putExtra("nameHere2", getIntent().getExtra("nameHere2"));
//just put extra's here
startActivity(intent);
finish();

Another solution is to NOT call finish(); in your baseActivity, your activities will look like:

baseActivity.java (this is the one with the ListView)

Intent intent = new Intent(this, detailView.class);
intent.putExtra("nameHere1", valueHere1);
intent.putExtra("nameHere2", valueHere2);
//just put extra's here
startActivity(intent);

detailView.java would do something like this in your onBackPressed:

finish();

In this approach, you don't "kill" the baseActivity so its values are still in there when the detailView comes on screen. However, I do not really support this approach as it takes up unnecessary RAM.

Razgriz
  • 7,179
  • 17
  • 78
  • 150
  • Thanks for your answer. The main thing was calling onBackPressed() method. But what I had done was defining the first detail view as the parent of the second detail view. I had to remove that and call finish() in onBackPressed() in my second detail view. – Loolooii May 19 '14 at 12:58