在旋转的Andr​​oid活动重新启动 [英] Activity restart on rotation Android

查看:129
本文介绍了在旋转的Andr​​oid活动重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,当我旋转设备(滑出键盘),然后我的活动重新开始(的onCreate C>被调用)。现在,这大概是它应该是,但我做了很多初始设置上的的onCreate 的方法,所以我需要两种:

  1. 将所有的初始设定在另一项功能,所以它不是所有的设备旋转或丢失
  2. 请这么的onCreate 不是再次调用和布局只是调整或
  3. 限制了应用程序只是画像,使的onCreate 不叫。
解决方案

使用应用程序类

根据你做你的初始化你可以考虑创建一个扩展的新类应用程序和移动你的初始code到重写的onCreate 方法类中。

 公共类MyApplicationClass扩展应用{
  @覆盖
  公共无效的onCreate(){
    super.onCreate();
    // TODO把你的应用程序初始化code在这里。
  }
}
 

的onCreate 在创建整个应用程序时,应用程序类仅调用,所以在活动上重新定位或键盘的可见性的改变不会触发它。

这是很好的做法,揭露这个类作为一个单独的实例,并揭露你使用getter和setter初始化应用程序变量。

注意:您需要在清单中注明您的新的应用程序类的名称为它注册和使用:

 <应用
    机器人:名称=com.you.yourapp.MyApplicationClass
 

反应到配置更改 [更新:这是因为API 13 pcated德$ P $; 看到建议的替代]

作为另一种选择,你可以有你的应用程序侦听事件会导致重新启动 - 样的方向和键盘的可见性的变化 - 和你的活动范围内处理它们

开始添加安卓configChanges 节点的活动的清单节点

 安卓configChanges =keyboardHidden |定位
 

的Andr​​oid 3.2(API级别13)和新

 安卓configChanges =keyboardHidden |方向|屏幕尺寸
 

那么活动中覆盖 onConfigurationChanged 方法,并调用的setContentView 强制GUI布局重新做在新的方向。

  @覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
  super.onConfigurationChanged(NEWCONFIG);
  的setContentView(R.layout.myLayout);
}
 

In my Android application, when I rotate the device (slide out the keyboard) then my Activity is restarted (onCreate is called). Now, this is probably how it's supposed to be, but I do a lot of initial setting up in the onCreate method, so I need either:

  1. Put all the initial setting up in another function so it's not all lost on device rotation or
  2. Make it so onCreate is not called again and the layout just adjusts or
  3. Limit the app to just portrait so that onCreate is not called.

解决方案

Using the Application Class

Depending on what you're doing in your initialization you could consider creating a new class that extends Application and moving your initialization code into an overridden onCreate method within that class.

public class MyApplicationClass extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    // TODO Put your application initialization code here.
  }
}

The onCreate in the application class is only called when the entire application is created, so the Activity restarts on orientation or keyboard visibility changes won't trigger it.

It's good practice to expose the instance of this class as a singleton and exposing the application variables you're initializing using getters and setters.

NOTE: You'll need to specify the name of your new Application class in the manifest for it to be registered and used:

<application
    android:name="com.you.yourapp.MyApplicationClass"

Reacting to Configuration Changes [UPDATE: this is deprecated since API 13; see the recommended alternative]

As a further alternative, you can have your application listen for events that would cause a restart – like orientation and keyboard visibility changes – and handle them within your Activity.

Start by adding the android:configChanges node to your Activity's manifest node

android:configChanges="keyboardHidden|orientation"

or for Android 3.2 (API level 13) and newer:

android:configChanges="keyboardHidden|orientation|screenSize"

Then within the Activity override the onConfigurationChanged method and call setContentView to force the GUI layout to be re-done in the new orientation.

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);
}

这篇关于在旋转的Andr​​oid活动重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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