Android TabActivity 和方向更改

发布于 2024-12-13 20:02:56 字数 395 浏览 0 评论 0原文

我有一个 TabActivity 托管其他三个活动。

如果选择第一个选项卡,我可以更改设备方向,一切正常。

如果我转到第二个或第三个选项卡并更改方向,选项卡子活动(第二个和第三个)的 onCreate() 方法将被调用两次,第一次是设置默认选项卡内容(第一个)和第二个 onCreate 正在设置选定的选项卡内容(第一次应该如此)。

为什么 onCreate() 方法被调用两次,我该如何解决这个问题?

编辑:

我不想在清单中使用 android:configChanges="orientation" 因为我想将应用程序标题和系统栏向下并纵向显示以显示它们,而这只有可能在设置内容之前...

I have a TabActivity that hosts other three activities.

If the first tab is selected, I can change the device orientation and everything is ok.

If I go to the second or the third tab and change the orientation, the tab sub activity's(second and third) onCreate() method gets called twice, for the first time is setting the default tab content(the first one) and at the second onCreate, is setting the selected tab content(as it should for the first time).

Why is onCreate() method being called twice and how can I solve the problem?

EDIT:

I don't want to use android:configChanges="orientation" in the manifest because I want to get the app title and system bar down and on portrait to show them, and this is possible only before setting a content...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何其悲哀 2024-12-20 20:02:56

使用清单文件中的 android:configChanges="orientation" 属性,如下所示

<activity android:name=".Settings" android:screenOrientation="portrait"
            android:configChanges="orientation"></activity>

use the android:configChanges="orientation" attribute in manifest file like below

<activity android:name=".Settings" android:screenOrientation="portrait"
            android:configChanges="orientation"></activity>
百思不得你姐 2024-12-20 20:02:56

为了让我清楚,您是说您有多个选项卡,它们使用不同的方向(纵向或横向),并且您在切换选项卡并正确设置相应方向时遇到问题?

回应 Cata 的评论:

是的,所以每当您旋转屏幕时,当前可见的活动都会被销毁,并再次调用 onCreate (如果我记得这些步骤)。您要做的就是调用 getCurrentTab(),它返回代表选项卡的 int 值,并在调用 onCreate 时将其重置为活动选项卡。您可以通过多种方式执行此操作...通过使用一个小方法来处理该问题并通过 onCreate 调用它,或者使用 onSaveInstanceState(Bundle) 保存当前数据,并使用 onRestoreInstanceState() 重新加载选项卡数据。

您可以设置一个全局 int (int currentTab = 0),而不是在 onCreate() 中设置它,并且在 onSaveInstanceState(Bundle) 方法中,您可以将其保存到当前选项卡 (currentTab = getCurrentTab()),然后在 onRestoreInstanceState() 中你可以重新设置它。

这有道理吗?

请记住,我尚未对此进行测试,但如果您不熟悉这两个方法调用,我愿意这样做。

下面是将数据保存到 Bundle 的示例 - 还记得 onCreate 接受该活动包作为参数。

@Override
public void onSaveInstanceState(Bundle outState){
    // Store UI state to the savedInstanceState.
    // This bundle will be passed to onCreate on next call.
    super.onSaveInstanceState(outState);
    String strMinSec = timer.getText().toString();
    String strMs = timerMs.getText().toString();
    long curElapstedTime = elapsedTime;
    boolean timerStopped = stopped;
    int orientation = this.getResources().getConfiguration().orientation;

    outState.putString("MinSec", strMinSec);
    outState.putString("Ms", strMs);
    outState.putLong("Elapsed", elapsedTime);
    outState.putBoolean("Stopped", timerStopped);
    outState.putInt("Orientation", orientation);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){

    // Restore UI state from the savedInstanceState.
    if (savedInstanceState != null){
        String MinSec = savedInstanceState.getString("MinSec");
        if (MinSec != null)
        {
          timer.setText(MinSec);
        }
        String Ms = savedInstanceState.getString("Ms");
        if (Ms != null)
        {
          timerMs.setText(Ms);
        }
        long elapsed = savedInstanceState.getLong("Elapsed");
        if(elapsed > 0)
            elapsedTime = elapsed;
        int theOrientation = savedInstanceState.getInt("Orientation");
        //if(theOrientation > 0)
            //this.setRequestedOrientation(theOrientation); 
    }
}

Just so that I am clear, are you saying that you have several tabs, which are using different orientations (portrait or landscape), and you are having issues when switching tabs and setting the corresponding orientations properly?

In response to Cata's comment:

Yes, so anytime you rotate the screen the currently viewable activity is destroyed and onCreate is called again (if I recall the steps). What you have to do is call getCurrentTab(), which returns the int value representing the tab, and resetting that as the active tab when onCreate is called. You can do this in several ways...either by having a small method that handles just that and calling it via onCreate, or by using onSaveInstanceState(Bundle) to save your current data, and onRestoreInstanceState() to reload your tab data.

You can set a global int (int currentTab = 0), not setting it in onCreate(), and in your onSaveInstanceState(Bundle) method you can save that to the current tab (currentTab = getCurrentTab()), then in onRestoreInstanceState() you can set it again.

Does that make sense?

Keep in mind that I have not tested that, but willing to do so if you aren't familiar with those two method calls.

Below is an example of saving data to the Bundle - also recall that onCreate accepts that activity bundle as a parameter.

@Override
public void onSaveInstanceState(Bundle outState){
    // Store UI state to the savedInstanceState.
    // This bundle will be passed to onCreate on next call.
    super.onSaveInstanceState(outState);
    String strMinSec = timer.getText().toString();
    String strMs = timerMs.getText().toString();
    long curElapstedTime = elapsedTime;
    boolean timerStopped = stopped;
    int orientation = this.getResources().getConfiguration().orientation;

    outState.putString("MinSec", strMinSec);
    outState.putString("Ms", strMs);
    outState.putLong("Elapsed", elapsedTime);
    outState.putBoolean("Stopped", timerStopped);
    outState.putInt("Orientation", orientation);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState){

    // Restore UI state from the savedInstanceState.
    if (savedInstanceState != null){
        String MinSec = savedInstanceState.getString("MinSec");
        if (MinSec != null)
        {
          timer.setText(MinSec);
        }
        String Ms = savedInstanceState.getString("Ms");
        if (Ms != null)
        {
          timerMs.setText(Ms);
        }
        long elapsed = savedInstanceState.getLong("Elapsed");
        if(elapsed > 0)
            elapsedTime = elapsed;
        int theOrientation = savedInstanceState.getInt("Orientation");
        //if(theOrientation > 0)
            //this.setRequestedOrientation(theOrientation); 
    }
}
奈何桥上唱咆哮 2024-12-20 20:02:56

我认为最好的解决方案是采用不同的方法,使用单个 Activity 并将每个选项卡内容放在不同的 LinearLayoutonClick() 中> 选项卡(将是一些按钮)在布局之间切换的方法...

如果有人有更好的想法,请在此处发布!

谢谢你!

I think the best solution is to take a different approach by using a single Activity and put every tab content in a different LinearLayout and in onClick() methods of the tabs(which will be some buttons) to switch between the layouts...

If anybody have a better idea, please post it here!

Thank you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文