【发布时间】:2013-04-06 06:55:48
【问题描述】:
我有一项主要活动。它以 Tab 启动另一个子活动。关闭子活动时,不会使用子选项卡释放内存。每次打开其他标签时,内存都会增加。
一段时间后,我的应用程序崩溃了:
public class Main extends Activity {
public static Activity activity;
private Bundle saveInstance;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.activity = this;
this.saveInstance = savedInstanceState;
Application app = Application.getSharedInstance();
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onBackPressed()
{
// super.onBackPressed();
AlertUtills.showQuitDialog(Main.this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
final TabHost tabHost = (TabHost) this
.findViewById(android.R.id.tabhost);
LocalActivityManager mLocalActivityManager = new LocalActivityManager(
this, false);
mLocalActivityManager.dispatchCreate(saveInstance);
tabHost.setup(mLocalActivityManager);
switch (item.getItemId())
{
case R.id.create_new:
{
if (CommonUtilities.isTabNotExist(tabHost, "NewProject"))
{
TabSpec spec2 = tabHost.newTabSpec("NewProject");
Intent in = new Intent(this, Activity_cretateNew.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec2.setContent(in);
spec2.setIndicator("New Project");
tabHost.addTab(spec2);
tabHost.setCurrentTabByTag("NewProject");
}
tabHost.setCurrentTabByTag("NewProject");
}
break;
case R.id.open:
{
OpenDialog dialog = new OpenDialog(this, "Open Project", false,
OpenDialogType.CORELOG_OPEN);
dialog.Show();
}
break;
case R.id.menu_exit:
onBackPressed();
break;
case R.id.import_las:
{
if (isCurrentProjectExist())
{
if (CommonUtilities.isTabNotExist(tabHost, "ImportLas"))
{
TabSpec spec2 = tabHost.newTabSpec("ImportLas");
Intent in = new Intent(this, LASImportWizard.class);
spec2.setContent(in);
spec2.setIndicator("Import Las");
tabHost.addTab(spec2);
}
tabHost.setCurrentTabByTag("ImportLas");
}
}
break;
case R.id.import_coredata:
{
if (isCurrentProjectExist())
{
if (CommonUtilities.isTabNotExist(tabHost, "coredata"))
{
TabSpec spec2 = tabHost.newTabSpec("coredata");
Intent in = new Intent(Main.this,
CoreDataImportWizard.class);
spec2.setContent(in);
spec2.setIndicator("Core Data Import");
tabHost.addTab(spec2);
}
tabHost.setCurrentTabByTag("coredata");
}
}
break;
case R.id.list_Data:
{
if (isProjectHasWell())
{
if (CommonUtilities.isTabNotExist(tabHost, "ListData"))
{
TabSpec spec1 = tabHost.newTabSpec("ListData");
Intent in = new Intent(Main.this, ListDataWindow.class);
// in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
spec1.setContent(in);
spec1.setIndicator("List Data");
tabHost.addTab(spec1);
}
tabHost.setCurrentTabByTag("ListData");
}
}
break;
case R.id.basemap:
{
if (isCurrentProjectExist())
{
if (CommonUtilities.isTabNotExist(tabHost, "BaseMap"))
{
TabSpec spec2 = tabHost.newTabSpec("BaseMap");
Intent inBasemap = new Intent(Main.this, Basemap.class);
spec2.setContent(inBasemap);
spec2.setIndicator("Base Map");
tabHost.addTab(spec2);
}
tabHost.setCurrentTabByTag("BaseMap");
}
}
break;
case R.id.logPlot:
{
if (isProjectHasWell())
{
if (CommonUtilities.isTabNotExist(tabHost, "LogPlot"))
{
TabSpec spec2 = tabHost.newTabSpec("LogPlot");
Intent intentLogPlot = new Intent(Main.this,
Logplot_Activity.class);
spec2.setContent(intentLogPlot);
spec2.setIndicator("Log Plot");
tabHost.addTab(spec2);
}
tabHost.setCurrentTabByTag("LogPlot");
}
}
break;
case R.id.show_hide_project_explorer:
{
FrameLayout frameLayout = (FrameLayout) activity
.findViewById(R.id.explorerFrame);
if (item.isChecked() == true)
{
item.setChecked(false);
frameLayout.setVisibility(View.GONE);
}
else
{
item.setChecked(true);
frameLayout.setVisibility(View.VISIBLE);
}
break;
}
}
return true;
}
private boolean isCurrentProjectExist()
{
return Application.getSharedInstance().getCurrentProject() == null ? false
: true;
}
private boolean isProjectHasWell()
{
return isCurrentProjectExist()
&& Application.getSharedInstance().getCurrentProject()
.getAllWells().length != 0 ? true : false;
}
public static Activity getMainActivity()
{
return activity;
}
}
【问题讨论】:
-
标签页中的托管活动已被弃用两年多。
-
请告诉我创建标签的其他方式。创建标签是我们的要求..
-
将片段与操作栏选项卡一起使用。或者使用带有
ViewPager和PagerTabStrip的片段。或使用FragmentTabHost。或者在常规TabHost中使用常规视图(而不是活动)。 -
我对您的回答感到非常困惑。我认为您建议的所有方式都与旧选项卡有相同的内存问题。所以我使用哪种方式??
-
“我认为您建议的所有方式都存在与旧选项卡相同的内存问题”——您对此的证明是,究竟是什么?您是否遵循 Gabe Sechan 的建议并检查您的堆以准确确定什么您的“内存问题”是什么?