In Activity.onCreate(), why does Intent.getExtras() sometimes return null?
This was probably a false alarm; it's more likely due to our own instance variable being null:
class ToActivity extends Activity {
public static final String SERVER_PARAM = "server";
public static final String UUID_PARAM = "uuid";
public static final String TEMPLATE_PARAM = "template";
private State state;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
state = initializeState();
// MISSING NULL CHECK HERE!
Bundle extras = getIntent().getExtras();
if (extras == null) {
finish();
return;
}
// do stuff with state and extras
}
}
Why Activity getIntent().getExtras() will return null sometimes?
This could be a buggy ACTION_SEND
implementation that failed to attach any extras. This could be some automated script — or script kiddie — manually invoking your activity without any extras.
Since the activity is exported, I recommend some "defensive programming". Do not assume the existence of an extras Bundle
. Instead, "gracefully degrade" as best you can in that case.
getIntent.getExtras() does not return null but has no values.
Try adding the extra variable to intent rather than in Bundle
Ex:
i.putExtra(KEY_1, a);
i.putExtra(KEY_2, b);
i.putExtra(KEY_3, c);
Then retrieve it from other activity from intent
Ex:
getIntent().getStringExtra(KEY_1) ;
getIntent().getExtras() sometimes crashing in Activity
This was a bug in AOSP which was fixed after Android 7. Here is the reason in the commit message:
Fix for race in writeToParcel and unparcel
Don't access the parcelled data while it might be recycled by another
thread.Also make a local reference of mMap, which could be modified by
another thread.
Intent.putExtras() just return null
You should aware that bundle is using key value pair, so if you create
bundle.putString("params", "okay");
the key is params
and value is okay
And to get the value you should use the same key.
String _getData = extras.getString("params");
Bundle extra is returning NULL
startActivityForResult()
's 3rd argument(Bundle option
) is not "extra" bundles.
See Context#startActivity(Intent, Bundle) for detail.
That is launch configuration.
use Intent#putExtra(String, boolean)
for boolean extras.
Intent intent = new Intent(Add_Product_Page.this, CategoryListActivity.class);
intent.putExtra("for_result", true);
startActivityForResult(intent, GET_CATEGORY);
then
boolean b = getIntent().getBooleanExtra("for_result", false);
This is equivalant to
boolean b = getIntent().getExtras().getBoolean("for_result");
Also you can check intent
has extra parameter or not:
intent.hasExtra("for_result");
Related Topics
Check Username and Password in Java Database and Give Wrong Password Message If False
Recyclerview Not Calling Oncreateviewholder or Onbindview
How to Find the Number of Times If or Else Code Has Executed
Select the Letters After - in a String
Using Nested While Loop to Print Pyramid of Stars
Autowiring Httpservletrequest in Spring Controller
How to Call an Excel Vba Macro from Java Code
How to Call a Function Only After the Previous Function Is Fully Executed in Java
Intellij Idea - Gradle: Execution Failed for Task ':Compilejava'
Java Spring @Scheduled Tasks Executing Twice
How to Encrypt a String/Stream With Bouncycastle Pgp Without Starting With a File
Filenotfoundexception in Src/Main/Resources
How to Know If Data Exists in a Firestore Query Snapshot
Parse Xml to Java Pojo in Efficient Way