Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:16:36+00:00 2026-06-14T09:16:36+00:00

I have this code that checks for a value of an extra in an

  • 0

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().getBoolean("isNewItem")

If isNewItem isn’t set, will my code crash? Is there any way to tell if it’s been set or not before I call it?

What is the proper way to handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:16:37+00:00Added an answer on June 14, 2026 at 9:16 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this code that checks 4 parameters (author, title, keyword and subject)
I have this code snippet inside a function that checks if an object exists
I have this code that reads from XML file. It gets five strings (groupId,
I have some code that checks for a value of say, var in the
i have this code that will check the array contains the specific string and
I have this code that will check for if the class more-results are in
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that runs but never stops. class A { public static
I have this code that suppose to place the marker by the latlng public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.