活动关闭时内存未被释放

时间:2013-04-06 06:55:49

标签: java android

我有一项主要活动。它以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;
}
}

1 个答案:

答案 0 :(得分:1)

运行hprof或其他类似的分析器。如果发生这种情况,标签活动的对象数量将会增加。看看持有它们的引用是什么,然后修复它们,使它们没有。

如果没有这些hprofs,很难调试这种事情。