gpt4 book ai didi

Android Eclipse,两个不同选项卡中的两个 Activity ,不断调用 onCreate()!

转载 作者:行者123 更新时间:2023-11-29 14:55:58 26 4
gpt4 key购买 nike

我有 2 个 Activity ,比如说 Activity1 和 Activity2。我已将这 2 个添加到 TabHost 下的 2 个单独的选项卡中。

每次我按所需的选项卡查看内容时,都会调用每个 Activity 的 onCreate() 并因此重新启动 Activity !这是为什么?我怎样才能防止这种情况发生??

谢谢。

来自 TabHostActivity 类的代码:

package zt.ztactive;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class TabHostActivity extends TabActivity {

TabHost tabHost;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabwindow);

/** TabHost will have Tabs */
tabHost = (TabHost)findViewById(android.R.id.tabhost);

/** TabSpec used to create a new tab.
* By using TabSpec only we can able to setContent to the tab.
* By using TabSpec setIndicator() we can set name to tab. */

/** tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid1");

/** TabSpec setIndicator() is used to set name for the tab. */
/** TabSpec setContent() is used to set content for a particular tab. */
firstTabSpec.setIndicator("First Tab Name").setContent(new Intent(this,Activity1.class));
secondTabSpec.setIndicator("Second Tab Name").setContent(new Intent(this,Activity2.class));

/** Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
}

}

最佳答案

你能展示一些关于你如何在你的 tabhost 中使用 Activities 的代码吗?理想情况下,一旦创建了选项卡, Activity 就会调用 onResume 而不是 onCreate,因为当您从一个选项卡移动到另一个选项卡时, Activity 不会被销毁。

关于Android Eclipse,两个不同选项卡中的两个 Activity ,不断调用 onCreate()!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6844502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com