Lobo
Lobo

Reputation: 183

Android getIntent().getExtras() NullPointerException

In my application i have three Activities : A,B,C.

Activity A includes listview with @OnItemClick that starts Activity B and sends Extras to it.

In Activity B i getExtras in OnCreate(), so everytime activity starts it tries to get those values. The problem is that they are not always there, for example when coming back from Activity C (because list item in Activity A was not clicked).

What would be the best way to deal with getting extras with this kind of workflow?

EDIT: The extras im sending are item list position.

Upvotes: 0

Views: 290

Answers (2)

Rafa
Rafa

Reputation: 3349

If what you're trying to do is get rid of the nullpointer error try checking to see if the key for the value you are trying to get exists.

Bundle extras = intent.getExtras();
if (extras != null) {
    if (extras.containsKey("isNewItem")) {
        boolean isNew = extras.getBoolean("isNewItem", false);

        // do something with these values
    }
}

Upvotes: 2

Hasan Saykın
Hasan Saykın

Reputation: 148

Pass your all extras to all of your activities A,B,C in order not to lose them.

Upvotes: 0

Related Questions