Activity类调用onConfigurationChange

Activity类调用onConfigurationChange

本文介绍了onCreate方法是在Activity类调用onConfigurationChanged方法之后调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android系统编程初学者,现在我工作的这显示默认排序顺序的数据,并提供搜索工具Android应用程序。为此,我写了一个活动叫搜索,将搜索内容并显示在列表视图。我的问题是,当我旋转设备Activity类的onCreate方法甚至overridding的onConfigurationChanged方法后调用。我也做了清单文件中的以下更改调用onConfigurationChanged方法。

I am a beginner in android programming, now i am working on an android application which displays the data in a sort order by default and also provides a search facility. For this i have written an activity called search that will search the contents and displays in a listview. My problem is when i rotate the device the onCreate method of the Activity class is called even after overridding the onConfigurationChanged method. I had also done the following changes in the manifest file to invoke the onConfigurationChanged method.

<activity
    android:name=".Search"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="sensor" >
</activity>

我的搜索活动如下:

My Search Activity is as follows :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  System.out.println("HERE AT ONCREATE");
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    System.out.println("HERE AT ON CONFIGURATION CHANGED");
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        setContentView(R.layout.search_port);
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.search_land);
    }
}

所以输出而旋转设备将是:

So the output while rotating the device will be :


  1. 在这里开配置不同

  1. HERE AT ON CONFIGURATION CHANGED

在这里的onCreate

HERE AT ONCREATE

我不希望得到同时转动装置叫做的onCreate()方法。请帮我解决这个问题。我使用的Andr​​oid 2.2 SDK与三星Galaxy Tab(7英寸显示屏)。

I dont want to get the onCreate() method called while rotating the device. Please help me to solve this issue. I am using Android 2.2 SDK with Samsung Galaxy Tab (7 inch display).

推荐答案

您正在尝试上述code要完成什么可以更简单地完成。

What you're trying to accomplish with the above code can be done much more simply.

如果你改变你的布局文件名字都是相同的: search_layout.xml 键,这种方式将它们放置在您的项目:

If you change your layout files name to be both the same: search_layout.xml and place them in your project this way :

<project>/res/layout/search_layout.xml       // Portrait Layout
<project>/res/layout-land/search_layout.xml  // Landscape Layout

然后修改你的活动是这样的:

And then modify your Activity this way :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("Search Activity", "onCreate() was called.");
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Log.i("Search Activity", "onConfigurationChanged was called.");
}

这就是我把它安装在我的活动和的onCreate()方法只调用一次。

这篇关于onCreate方法是在Activity类调用onConfigurationChanged方法之后调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:39