How do I tell if Intent extras exist in Android?

前端 未结 5 652
你的背包
你的背包 2020-12-23 09:46

I have this code that checks for a value of an extra in an Intent on an Activity that is called from many places in my app:

getIntent().getExtras().getBoolea         


        
相关标签:
5条回答
  • 2020-12-23 09:51

    As others have said, both getIntent() and getExtras() may return null. Because of this, you don't want to chain the calls together, otherwise you might end up calling null.getBoolean("isNewItem"); which will throw a NullPointerException and cause your application to crash.

    Here's how I would accomplish this. I think it's formatted in the nicest way and is very easily understood by someone else who might be reading your code.

    // You can be pretty confident that the intent will not be null here.
    Intent intent = getIntent();
    
    // Get the extras (if there are any)
    Bundle extras = intent.getExtras();
    if (extras != null) {
        if (extras.containsKey("isNewItem")) {
            boolean isNew = extras.getBoolean("isNewItem", false);
    
            // TODO: Do something with the value of isNew.
        }
    }
    

    You don't actually need the call to containsKey("isNewItem") as getBoolean("isNewItem", false) will return false if the extra does not exist. You could condense the above to something like this:

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        boolean isNew = extras.getBoolean("isNewItem", false);
        if (isNew) {
            // Do something
        } else {
            // Do something else
        }
    }
    

    You can also use the Intent methods to access your extras directly. This is probably the cleanest way to do so:

    boolean isNew = getIntent().getBooleanExtra("isNewItem", false);
    

    Really any of the methods here are acceptable. Pick one that makes sense to you and do it that way.

    0 讨论(0)
  • 2020-12-23 10:00

    getIntent() will return null if there is no Intent so use...

    boolean isNewItem = false;
    Intent i = getIntent();
    if (i != null)
        isNewItem = i.getBooleanExtra("isNewItem", false);
    
    0 讨论(0)
  • 2020-12-23 10:02

    The problem is not the getBoolean() but the getIntent().getExtras()

    Test this way:

    if(getIntent() != null && getIntent().getExtras() != null)
      myBoolean = getIntent().getExtras().getBoolean("isNewItem");
    

    by the way, if isNewItem doesn't exist, it returns de default vaule false.

    Regards.

    0 讨论(0)
  • 2020-12-23 10:06

    It will not crash unless until you use it! You don't have to get it if it exists but if you try, for some reason, to use a an "extra" which doesn' exists your system will crash.

    So, try o do something like:

    final Bundle bundle = getIntent().getExtras();
    
    boolean myBool=false;
    
    if(bundle != null) {
        myBool = bundle.getBoolean("isNewItem");
    }
    

    This way you make sure your app won't crash. (and make sure you have a valid Intent :))

    0 讨论(0)
  • 2020-12-23 10:14

    You can do this:

    Intent intent = getIntent();
    if(intent.hasExtra("isNewItem")) {
       intent.getExtras().getBoolean("isNewItem");
    }
    
    0 讨论(0)
提交回复
热议问题