繁体   English   中英

不要使用TabLayout将Activity保留在缓存中

[英]Don't keep Activity in cache with TabLayout

这是我的应用程序结构:我有一个带有4个标签的主活动(帐户,仪表板,媒体,应用程序)。 每个选项卡都链接到一个活动。 “应用程序”选项卡包含许多活动,为了使这些选项卡可见,我在“ ApplicationsActivity”中执行if / else来知道必须显示的布局(可能不是最好的方法,但是我没有发现任何更好的方法) )。

我的问题:当进入“应用程序”选项卡时,我看到所有应用程序的列表,然后单击一个应用程序,得到一个包含该应用程序选项的新屏幕,单击一个选项,得到一个新屏幕,依此类推然后,单击“媒体”选项卡,我将获得所有媒体的列表。 然后,我再次单击“应用程序”选项卡,仍然在屏幕上显示特定应用程序的选项。 我要返回的是所有应用程序列表。

我怎样才能做到这一点 ? 我该怎么做才能告诉选项卡它必须重新加载活动,而不是将其放入似乎是缓存的内容中?

这是我的主要活动代码:

public class MainActivity extends TabActivity {
private int currentTab = 1;
private Application application = null;
private String optionType = null;
private String checkID = null;
private String alertID = null;
private int periodID = -1;
private Statistic stat = null;
private Media media = null;
TabHost tabHost;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    PreferencesManager.load(this);

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        Log.i("IV", bundle.toString());

        String applicationID = null;
        applicationID = bundle.getString("applicationID");
        if (applicationID != null) {
            try {
                this.application = Utils.getApplication(applicationID);
            } catch (AuthenticationFailedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ApiCallErrorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InternalErrorException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            this.application = (Application) bundle
                    .getSerializable("application");
        }

        this.currentTab = bundle.getInt("currentTab", 1);
        this.optionType = bundle.getString("optionType");
        this.checkID = bundle.getString("checkID");
        this.alertID = bundle.getString("alertID");
        this.periodID = bundle.getInt("periodID", -1);
        this.stat = (Statistic) bundle.getSerializable("stat");
        this.media = (Media) bundle.getSerializable("media");
    }

    Log.i("IV", "current tab : " + currentTab);

    Resources res = getResources(); // Resource object to get Drawables
    tabHost = getTabHost(); // The activity TabHost

    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        public void onTabChanged(String tabId) {
            //Log.i("IV", "Tab id : " +tabId);
            setTabColor(); 
        }
    });

    TabHost.TabSpec spec; // Resusable TabSpec for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    Intent intent = new Intent().setClass(this, AccountActivity.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost
            .newTabSpec("account")
            .setIndicator(getString(R.string.account),
                    res.getDrawable(R.drawable.tab_account))
            .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, DashboardActivity.class);
    spec = tabHost
            .newTabSpec("dashboard")
            .setIndicator(getString(R.string.dashboard),
                    res.getDrawable(R.drawable.tab_dashboard))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, MediasActivity.class).putExtra(
            "media", media);
    spec = tabHost
            .newTabSpec("medias")
            .setIndicator(getString(R.string.medias),
                    res.getDrawable(R.drawable.tab_media))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, ApplicationsActivity.class)
            .putExtra("application", application)
            .putExtra("optionType", optionType)
            .putExtra("checkID", checkID).putExtra("alertID", alertID)
            .putExtra("periodID", periodID).putExtra("stat", stat);
    spec = tabHost
            .newTabSpec("applications")
            .setIndicator(getString(R.string.applications),
                    res.getDrawable(R.drawable.tab_application))
            .setContent(intent);
    tabHost.addTab(spec);

    setTabColor();

    if (!PreferencesManager.getBoolean("isLogged")) {
        for (int i = 1; i < 4; i++) {
            tabHost.getTabWidget().getChildTabViewAt(i).setEnabled(false);
            tabHost.getTabWidget().getChildTabViewAt(i)
                    .setBackgroundColor(Color.rgb(102, 102, 102));
        }
        tabHost.setCurrentTab(0);
    } else {
        tabHost.setCurrentTab(currentTab);
    }
}

public void setTabColor() {
    for(int i=0;i<this.tabHost.getTabWidget().getChildCount();i++)
    {
        this.tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000")); //unselected
    }
    this.tabHost.getTabWidget().getChildAt(this.tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#ABABAB")); // selected
}
}

在我的ApplicationsActivity中,我只对捆绑包中的值进行if / else操作,以了解与setContentView()一起使用的布局;

非常感谢你 !

我不知道,如果了解您的问题,但也许尝试在android清单文件中更改您的活动设置并添加:

android:noHistory="true"

您是否曾经尝试在TabActivity中使用ActivityGroup? 使用ActivityGroup,您可以轻松摆脱整个if-else事情。

这实际上可以解决您的问题: 返回上一个TabActivity

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM