Check if extras are set or not
Asked Answered
M

6

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.

Micturition answered 24/11, 2011 at 10:33 Comment(4)
You can use if(extras.getString("extra") == null) {extraStr = "extra not set";}. In your code NullPointerException occurs at String extraStr = extras.getString("extra").Dilworth
Not really. NullPointerException is throwing at extras.getString("extra") not when assigning it to extraStr. So the solution is what Michal Kottman said.Micturition
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.Dilworth
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.Micturition
A
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.

Acroterion answered 24/11, 2011 at 10:35 Comment(1)
Intent.hasExtra(String name) in kotlin crashing the app giving null pointer exceptionOxidase
G
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");
        }
    }
Gather answered 31/3, 2015 at 16:26 Comment(1)
!bd.getString("nomeUsuario").equals(null) seems like a bug. It'd throw an NPE if the value was e.g. a Number.Rugged
M
7
if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
   // intent is not null and your key is not null
}
Much answered 22/1, 2015 at 13:55 Comment(0)
C
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";
    }
Caniff answered 19/8, 2016 at 2:14 Comment(0)
F
5

I would use this solution in your case.

String extraStr;
    try {
        extraStr = getIntent().getExtras().getString("extra");
    } catch (NullPointerException e ) {
        extraStr = "something_else";
    }
Fourgon answered 11/2, 2016 at 16:10 Comment(0)
J
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();
                }
            }
        }
    }
Juggler answered 28/9, 2021 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.