免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1304 | 回复: 0
打印 上一主题 下一主题

Orientation & Application [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-06 12:59 |只看该作者 |倒序浏览

                                                                                                                                                                By default, when screen orientation is changed, Activity will destructed and recreated, if you want to store some state before it is destructed, and restored it after it is recreated, you can overload  functions onSaveInstanceState and onRestoreInstanceState of Activity as following:
    public void onSaveInstanceState(Bundle outState)
    {
        //---store whatever you need to persist—
        outState.putString("MyPrivateID", "1234567890");
        super.onSaveInstanceState(outState);
    }
When an Activity is recreated, the onCreate event is fired first, followed by the onRestoreInstanceState event:
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        //---retrieve the information persisted earlier---
        String strPrivateID = savedInstanceState.getString("MyPrivateID");   
    }
Via one of following information, we can prevent Activity to be destructed and recreated when screen orientation is changed:
1) Declare fixed screen orientation in manifest:
  android:screenOrientation="sensor"  
                  android:label="@string/app_name">
the value of android:screenOrientation can be: portrait, landscape, sensor(Automatically adjust)
2) Declare to bypass Activity Destruction
android:configChanges="keyboardHidden|orientation"
                  android:label="@string/app_name">
If you need to get notification when screen orientation is changed, you can overload the function onConfigurationChanged of class Activity, or register an orientation listener (Define a class which extends OrientationListener, instance that class in Activity.onCreate, and call function enable() of the defined orientation listener class), i.e.
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.xxxx);  //Set new content view
    /*
     If you used theme in manifest, it is impossible to change to other theme except in onCreate(), so if the used theme contains background images, it will be stretched when screen orientation is changed, but you can change them (background image) by calling getWindow().setBackgroundDrawableResource(R.drawable.xxxxxx);
    */
}
More information, please refer to online document:
Title: "Developing Orientation-Aware Android Applications"
Address: http://www.devx.com/wireless/Article/40792/1954
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/9577/showart_1853549.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP