I am trying to use android:configChanges="keyboardHidden|orientation|screenSize"
in my Manifest so that I cannot lose state of my VideoView
when configuration changes happen. This means I need to choose the layouts myself on the configuration. The problem I am having is that when I first initialize which view to use in onCreate
, here:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_make_photo_video_land);
} else if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_make_photo_video);
}
}
It chooses the right layout. But when I need to adjust the layout on the configuration change like here:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_make_photo_video_land);
} else if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.activity_make_photo_video);
}
}
It does not use the same instance of the layout, but rather reloads it, which makes my VideoView
go black. Then I am unable to press any buttons after that.
Is there a way to reuse the initial layouts? I have tried to set the views like this one setContentView(R.layout.activity_make_photo_video_land);
to an Activity activity
object, but IDE won't let me, it says Activity is incompatible with void
. Because if I could get a reference to that view, then I could reuse it. Or is there a simpler way of doing this that I am not seeing?
Thanks.