处理屏幕方向 - 机器人 [英] Handling Screen Orientation - Android

查看:95
本文介绍了处理屏幕方向 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序具有屏幕orienation的一个问题。我在RES /布局-LAN文件夹中创建一个备用布局横向模式。中方向发生变化的问题​​,

1.An活动得到重建,不破坏原有的业务。

2.since我用我的应用程序媒体播放器,在屏幕旋转的.MP3播放两个方向同时..

我无法找到合适的采样code处理屏幕方向与保存和恢复活动状态..............

解决方案
  

1.An活动得到重建,不破坏原有的业务。

活动的重建是Android系统,当发生配置更改的自然默认行为。旧的活动萦绕在内存中可能的原因是因为它引用当前正在播放的实例的MediaPlayer

由于您使用的是不同的布局资源进行横向和纵向,它是你的优势,让Android的重建活动,每一次拉动相应的资源。如果手柄转动自己,你将负责重新加载适当的布局,以及。

  

2.since我用我的应用程序媒体播放器,在屏幕旋转的.MP3播放两个方向同时..

有两种办法可以解决这个问题...

理想的解决办法是将你的媒体播放成服务。该活动可拨打服务启动/停止/等。回放时由用户控制的,但是把这个变成像一个背景组件服务允许它继续,即使操作,当你的活动是在不断变化,由于变化。这是设计模式的Andr​​oid团队鼓励,你的活动真的只涉及用户界面。

另一种可行的解决方案是通过你的的MediaPlayer 从旧的活动到新的使用 onRetainNonConfigurationInstance()。这使得单个的MediaPlayer 活动之间存在着情况下,保持播放是一致的。例如:

 公共类MyActivity延伸活动{
    私人MediaPlayer的MPLAYER;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        //检查如果我们从最后一个实例传递球员
        MPLAYER =(MediaPlayer的)getLastNonConfigurationInstance();
        //如果没有,使一个新的
        如果(MPLAYER == NULL){
            MPLAYER =新的MediaPlayer();
           //...Set新的播放器实例...
        }
    }

    @覆盖
    公共对象onRetainNonConfigurationInstance(){
        //清除我们的成员变量来保证此活动
        //允许GC后的onDestroy()
        MediaPlayer的实例= Mplayer的;
        MPLAYER = NULL;
        要创建//手我们的当前玩家到下一个活动
        返回实例;
    }

}
 

另一种选择,以确保最佳的内存清理是定义 MPLAYER 的WeakReference< MediaPlayer的> 允许GC的要求旧活动,即使的MediaPlayer 播放音频的配置更改的时间。

I am having a problem in screen orienation in my application. I created an alternate layout in res/layout-lan folder for landscape mode. The problem occurred during orientation change,

1.An activity get recreated without destroying the old activity.

2.since i am using mediaplayer in my app,on screen rotation the .mp3 is playing on both orientation concurrently..

I am unable to find proper sample code for handling screen orientation with saving and restoring the state of the activity..............

解决方案

1.An activity get recreated without destroying the old activity.

The recreation of the Activity is the natural default behavior of Android when a configuration change occurs. The likely reason your old Activity lingers in memory is because it is referencing a currently playing instance of a MediaPlayer.

Because you are using different layout resources for landscape and portrait, it is to your advantage to let Android recreate the Activity and pull the appropriate resources each time. If you handle rotation yourself, you will be responsible to reloading the proper layout as well.

2.since i am using mediaplayer in my app,on screen rotation the .mp3 is playing on both orientation concurrently..

There are two solutions to this issue...

The ideal solution is to move your media playback into a Service. The Activity can call the Service to start/stop/etc. playback when directed by the user, but putting this into a background component like a Service allows it to operate continuously even when your Activity is in flux due to the changes. This is the design pattern the Android team encourages, where your Activity really only deals with user interface.

Another workable solution is to pass your MediaPlayer from the old Activity to the new one using onRetainNonConfigurationInstance(). This allows the single MediaPlayer to exist between Activity instances, keeping the playback consistent. For example:

public class MyActivity extends Activity {
    private MediaPlayer mPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Check if we have a player passed in from the last instance
        mPlayer = (MediaPlayer)getLastNonConfigurationInstance();
        //If not, make a new one
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
           //...Set up new player instance...
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        //Clear our member variable to guarantee this Activity
        // is allowed to GC after onDestroy()
        MediaPlayer instance = mPlayer;
        mPlayer = null;
        //Hand our current player up to the next Activity to be created
        return instance;
    }

}

Another option to ensure the best memory cleanup would be to define mPlayer as a WeakReference<MediaPlayer> to allow the GC to claim the old Activity, even if the MediaPlayer is playing audio at the time of the configuration change.

这篇关于处理屏幕方向 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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