SSS.remi
SSS.remi

Reputation: 23

Getting intent extras from just one class

Is there a way I can just receive intent extras from just one class? For example I have two activities that use intent to open the third activity. Now I just want one of the first two to pass extras so I can receive it in the third activity.

I have implemented everything but it crashes because when I open third activity from second one that doesn't put extras, it crashes my app.

To picture it more clearly:

Both Activity 1 and 2 use intent to start Activity 3, but I just need extras from activity 1.

Upvotes: 0

Views: 295

Answers (2)

Rohit5k2
Rohit5k2

Reputation: 18112

Just put a null check

Intent intent = getIntent();
Bundle data = intent.getExtras();
if(data != null)
{
    String one = intent.getString("data_one", null); // use your data type
}
else
{
    // No extra received
}

Upvotes: 1

Shark
Shark

Reputation: 6610

Maybe something as simple as this?

if(getIntent() != null && getIntent().getExtras() != null)
{
  //presume this is Activity1, get extras that we need
  ...
} else {
  //any non-Activity1 activity launched it
}

Upvotes: 0

Related Questions