开发者

Changing android:configChanges dynamically

开发者 https://www.devze.com 2023-04-01 14:15 出处:网络
I am creating a magazine app which uses a ViewPager and scrolls over different WebViews. Most of the pages are portrait but some of them can be seen as landscape.

I am creating a magazine app which uses a ViewPager and scrolls over different WebViews. Most of the pages are portrait but some of them can be seen as landscape.

To handle this, I am using two methods in the Activity holding the ViewPager:

public void allowOrientationChanges() {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}

public void enforcePortrait() {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

With this we make sure that all the pages that just need to be portrait stay portrait (calling getActivity().enforcePortrait()) and the ones that have portrait/landscape views goes through the whole restart process (calling getActivity().allowOrientationChanges()).

We reached a point where we want to add video to some of the magazine's pages. Unfortunately the HTML5 video tag isn't working fine so we added a VideoView to our layout. The feature to add now is:

  • in portrait: Showing the video in a certain place. (Done!)
  • in landscape: Making the video full screen (like the youtube app).

I have been trying to do this and the only way I found is changing the size of the VideoView in the onConfigurationChanged() method in the Activity and notifying the fragment that the configuration changed. The problem with this is adding android:configChanges="orientation|keyboardHidden" to the Activity will "disallow" the views to be recreated.

The two possible solutions I开发者_JAVA百科 thought of:

  1. Changing the android:configChanges from the code and depending on the page but I couldn't find if that call exists.
  2. Forcing a recreate on the onConfigurationChanged() method. I don't know how to force a recreation of the fragment.

Any ideas?


Maybe there is a way around it by defining the activity twice in the manifest with similar names and setting the second incarnation to enabled=false. Then switch the 2 activities around dynamically by calling PackageManager.setComponentEnabledSetting(....) it is just an idea though, not sure it if will work.


I am creating a magazine app which uses a ViewPager and scrolls over different WebViews.

Is this reliable?

Most of the pages are portrait but some of them can be seen as landscape.

Please allow all pages to be viewed in landscape. If your users want to view your app in landscape -- for example, they do not feel like turning their television on its side just to use your app -- please let them.

Any ideas?

In onResume() of the activity, have it notify the magazine page fragment to fix up its VideoView size/position based on the then-current Configuration (getResources().getConfiguration()).


I just fixed it. Thanks for your answers.

My solution was to make the Activity have the android:configChanges="orientation|keyboardHidden" flag.

After doing that I override the onConfigurationChanged() method inside the Fragment where I resize the WebView or the VideoView depending on what I need.

EDIT:

I just did a blog post about this.

0

精彩评论

暂无评论...
验证码 换一张
取 消