Check if extras are set or not

AndroidAndroid ActivityAndroid IntentBundle

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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");
        }
    }

Solution 3 - Android

if (this.getIntent().getExtras() != null && this.getIntent().getExtras().containsKey("yourKey")) {
   // intent is not null and your key is not null
}

Solution 4 - Android

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";
    }

Solution 5 - Android

I would use this solution in your case.

String extraStr;
    try {
        extraStr = getIntent().getExtras().getString("extra");
    } catch (NullPointerException e ) {
        extraStr = "something_else";
    }

Solution 6 - Android

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();
                }
            }
        }
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjalv1039View Question on Stackoverflow
Solution 1 - AndroidMichal KottmanView Answer on Stackoverflow
Solution 2 - AndroidJúlioCézarView Answer on Stackoverflow
Solution 3 - AndroidRenato ProbstView Answer on Stackoverflow
Solution 4 - AndroidtvtruongView Answer on Stackoverflow
Solution 5 - AndroidExideView Answer on Stackoverflow
Solution 6 - AndroidKumar SantanuView Answer on Stackoverflow