适当地处理活动启动时屏幕方向变化 [英] Gracefully handling screen orientation change during activity start

查看:123
本文介绍了适当地处理活动启动时屏幕方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法,妥善处理设置在其中它的方向是从启动它的意图数据来确定的活动。这是一种游戏,用户可以选择的水平,其中一些是INT纵向的,有些是横向方向。我现在面临的问题是, setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)不生效,直到活动是满载。这是一个问题,对我来说,因为我的启动,这是我想只有这样做一次,在做一些装载和图像处理。

I'm trying to find a way to properly handle setting up an activity where its orientation is determined from data in the intent that launched it. This is for a game where the user can choose levels, some of which are int portrait orientation and some are landscape orientation. The problem I'm facing is that setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) doesn't take effect until the activity is fully loaded. This is a problem for me because I do some loading and image processing during startup, which I'd like to only have to do once.

目前,如果用户选择了景观水平:

Currently, if the user chose a landscape level:

  • 在活动开始的onCreate(),默认为纵向
  • 从分析其启动意图发现,它应该是在横向
  • 在继续不顾一路onResume(),加载信息,并执行其他安装任务
  • 在这样的应用程序这一点setRequestedOrientation踢通过的onPause()来的onDestroy()运行
  • 在其后再由的onCreate()启动并运行到onResume()重复早期
  • 设置

有没有办法避免这一点,有它不执行装载两次?例如,理想情况下,该活动会知道以前更加的onCreate调用它是否应该是横向或纵向取决于发射意向的某些属性,但除非我已经错过了的东西,是不可能的。我已经成功地破解在一起的方式,以避免通过检查布尔重复加载前的耗时装载的步骤,但是这似乎并不像正道正在做。我想我可以重写的onSaveInstanceState ,但这需要很多额外的编码。有一个简单的方法来做到这一点?

Is there a way to avoid that and have it not perform the loading twice? For example, ideally, the activity would know before even onCreate was called whether it should be landscape or portrait depending on some property of the launching intent, but unless I've missed something that isn't possible. I've managed to hack together a way to avoid repeating the loading by checking a boolean before the time-consuming loading steps, but that doesn't seem like the right way of doing it. I imagine I could override onSaveInstanceState, but that would require a lot of additional coding. Is there a simple way to do this?

谢谢!

解决方法:

根据丹尼尔的回答,这实际上是很容易解决。我只是需要做一些小的改动。在我的菜单活动,玩家会选择玩哪个级别,我不得不添加的if / else检查选择哪些类将被我的意图来启动。这是一个简单的INT重新presenting纵向或横向完成,确定当玩家选择了一个级别。然后,我创建了一个二等延长我的GameLogic级;这是其中所含大部分的code表示对游戏本身的类,而不是菜单,指令等等。

As per Daniel's answer, this was actually quite easy to fix. I just needed to make a few small changes. In my 'menu' Activity, where the player would choose which level to play, I just had to add an if/else check to choose which class would be started by my Intent. This was done with a simple int representing portrait or landscape, determined when the player selected a level. I then created a second class extending my 'GameLogic' class; this is the class which contained most of the code for the game itself, rather than the menus, instructions, etc.

public class GameLandscape extends GameLogic{
}

字面上那么简单,完全是空的。这样,它继承了所有的code从我的previous活动在那里我已经codeD它来处理事情取决于方向不同。最后我不得不添加一行到清单,说明GameLandscape总是在横向运行,并且GameLogic总是在纵向运行。

Literally that simple and completely empty. That way it inherited all the code from my previous activity where I had already coded it to handle things differently depending on the orientation. Lastly I just had to add a line to the manifest stating that GameLandscape would always run in landscape, and GameLogic would always run in portrait.

因此​​,一个简单的问题,确实如此。

So a simple problem indeed.

推荐答案

您也可以创建两个活动 - 一个是纵向的水平,其他的景观水平 - 然后将活动的方向在AndroidManifest.xml中,使用<一个href="http://developer.android.com/intl/fr/guide/topics/manifest/activity-element.html#screen"><$c$c>android:screenOrientation属性。你甚至不会有,如果你使用继承复制code;使用当前的活动为基础的活动,只是建立横向/纵向活动作为活动的子类。

You could make two Activities - one for portrait levels, the other for landscape levels - and then set the Activity's orientation in AndroidManifest.xml, using the android:screenOrientation attribute. You won't even have to duplicate code if you use inheritance; use your current Activity as the base activity, and just create the landscape/portrait Activities as subclasses of that Activity.

我想一个更好的解决办法是在意向打开这两个正确的活动,但如果你必须拥有一切可以通过意向额外的分析路由,你可以将所有层次到第三个活动,做比分析而已的意图,然后转发给适当的活动。

I think a better solution would be for the Intent to open the correct Activity of these two, though if you must have everything be routed via Intent extra analysis, you could forward all levels to a third Activity that does nothing more than analyse the Intent and then forward it to the proper Activity.

这篇关于适当地处理活动启动时屏幕方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆