How do I tell if Intent extras exist in Android?

前端 未结 5 661
你的背包
你的背包 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 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 :))

提交回复
热议问题