67

Is there any way to check if an extra has been passed when starting an Activity?

I would like to do something like (on the onCreate() in the Activity):

    Bundle extras = getIntent().getExtras();
    String extraStr = extras.getString("extra");

    if (extraStr == null) {
        extraStr = "extra not set";
    }

But this is throwing a java.lang.NullPointerException.

Thank you.

jalv1039
  • 1,663
  • 3
  • 17
  • 21
  • You can use if(extras.getString("extra") == null) {extraStr = "extra not set";}. In your code NullPointerException occurs at String extraStr = extras.getString("extra"). – Pankaj Kumar Nov 24 '11 at 10:42
  • 1
    Not really. `NullPointerException` is throwing at `extras.getString("extra")` not when assigning it to `extraStr`. So the solution is what Michal Kottman said. – jalv1039 Nov 24 '11 at 11:04
  • Have you read my comment carefully? What code I have written in my if condition? It clearly saying that the same as you are teaching me. And in second part of my comment, I was indicating that you exception occurs at that line. – Pankaj Kumar Nov 24 '11 at 11:22
  • I think I haven't explained very well. What I wanted to say is that the `NullPointerException` error is throwing inside the `extras.getString("extra")` call. This call doesn't return me a `null` value, it just throws the error before returning any result. So the check makes no sense because it crashes before. I hope it is clear now. – jalv1039 Nov 30 '11 at 00:55

6 Answers6

209

Use the Intent.hasExtra(String name) to check if an extra with name was passed in the intent.

Example:

Intent intent = getIntent();

if (intent.hasExtra("bookUrl")) {
    bookUrl = b.getString("bookUrl");
} else {
   // Do something else
}

Also, use Intent.getStringExtra(String name) directly on the intent to handle the NullPointerException if no extras were passed.

Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
13

Well, I had similiar problem. in my case the null point exception was happen when I checked if my bundle.getString() was equall to null.

here is how IN MY CASE I solved it:

Intent intent = getIntent();        
    if(intent.hasExtra("nomeUsuario")){
        bd = getIntent().getExtras();
        if(!bd.getString("nomeUsuario").equals(null)){
            nomeUsuario = bd.getString("nomeUsuario");
        }
    }
JúlioCézar
  • 447
  • 5
  • 8
  • `!bd.getString("nomeUsuario").equals(null)` seems like a bug. It'd throw an NPE if the value was e.g. a Number. – fncomp Mar 02 '18 at 16:04
7
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
   // intent is not null and your key is not null
}
Renato Probst
  • 5,914
  • 2
  • 42
  • 45
6

I think you need to check when extras != null

Bundle extras = getIntent().getExtras();
   if (extras != null) {
        String extraStr = extras.getString("extra");
    }else {
        extraStr = "extra not set";
    }
Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
tvtruong
  • 99
  • 1
  • 4
5

I would use this solution in your case.

String extraStr;
    try {
        extraStr = getIntent().getExtras().getString("extra");
    } catch (NullPointerException e ) {
        extraStr = "something_else";
    }
Exide
  • 61
  • 1
  • 7
0

Working Code

If you want to check first Intent have no extra

 Intent intent = getIntent();
        if (intent.getExtras() == null){
            startActivity(new Intent(Splash.this, Main.class));
            overridePendingTransition(R.anim.enter, R.anim.exit);
            finish();
        }else {
            if (intent.hasExtra("type")) {
                String type = getIntent().getStringExtra("type");

                switch (type){
                    case "showRateUsDialog":
                        Intent i = new Intent(Splash.this, Main.class);
                        i.putExtra("type", "showRateUsDialog");
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                        overridePendingTransition(R.anim.enter, R.anim.exit);
                        finish();
                        break;
                    case "refer":
                        Intent i2 = new Intent(Splash.this, Refer.class);
                        i2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i2);
                        overridePendingTransition(R.anim.enter, R.anim.exit);
                        finish();
                        break;
                    default:
                        startActivity(new Intent(Splash.this, Main.class));
                        overridePendingTransition(R.anim.enter, R.anim.exit);
                        finish();
                }
            }
        }
    }
Kumar Santanu
  • 603
  • 1
  • 7
  • 14