Check if extras are set or not

前端 未结 5 1382
余生分开走
余生分开走 2020-12-13 01:28

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):<

5条回答
  •  温柔的废话
    2020-12-13 02:04

    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.

提交回复
热议问题