Skip to content Skip to sidebar Skip to footer

Intent Extras Null On Configuration Change

I created a layout that displays on a SurfaceView and I can get the setDataSource by using Bundle extras = getIntent().getExtras(). Everything works fine until I try to set the la

Solution 1:

which gets the value from

Bundle extras = getIntent().getExtras();

Not always - if savedInstanceState is not null, then newString is the value of savedInstanceState.getSerializable("urlAddress");, which could possibly return null.

Alternatively, getIntent().getExtras() is null, therefore you hit

if (extras == null) {
    newString = null;
}

Which will definitely cause an error.

In either case, you can catch the error by using this

if (newString != null) {
    urlString = "rtsp://" + newString.trim().substring(2);
    // Code that requires urlString
    playAll();
} else {
    // Show an error
}

And, then to address the problem, you might have to implement onSaveInstanceState to put the url string into that savedInstanceState Bundle. But, you should be using putString and getString, probably, instead of put / get - Serializable. That way you avoid the cast.

In order to find where the variable is getting null, it's just a matter of logging and debugging appropriately.

Approaches to saving your data between orientation changes can be found at Handling Runtime Changes


Solution 2:

So I decided to make it manually

I use

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

    setContentView(R.layout.activity_live_alternate_land);

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();

    setContentView(R.layout.activity_live_alternate);
}

with this setting on AndroidManifest.xml

android:configChanges="orientation"

This will make the height/width value different without destroying the activity


Post a Comment for "Intent Extras Null On Configuration Change"