Android标签中片段的BackStack实现

时间:2012-11-23 21:29:05

标签: android tabs android-fragments out-of-memory

我的Android应用程序需要Iphone有点设计,比如有标签和多个内部屏幕。我完成了应用程序开发,如果我长时间使用该应用程序,现在面临OutOfMemoryError。所以我做谷歌搜索,发现在Tabs中设置活动将消耗更多的内存,我在FragmentTransactions中使用replace()来更改碎片。因此,当单击后退键时,会重新创建片段视图,这些视图在我的应用程序中非常昂贵,也会消耗大量内存。所以我想通过直接将视图设置为选项卡并为片段导航实现自己的后台堆栈来更改我的应用程序架构。我已经实现了以下代码,它工作正常,但使用show / Hide整个应用程序流程是好还是不好?有人可以建议我在下面的代码中实现backstack或纠正我的例子。

package com.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity implements OnTabChangedListener {
    ArrayList<Fragment> alTab1 = new ArrayList<Fragment>();
    ArrayList<Fragment> alTab2 = new ArrayList<Fragment>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    TabHost tabHost = findViewById(android.R.id.tabhost);
    tabHost.setup();
        // Tab for Photos
        TabSpec photospec = tabHost.newTabSpec("Photos");
        // setting Title and Icon for the Tab
        photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
        photospec.setContent(R.id.tab1content);
        // Tab for Videos
        TabSpec videospec = tabHost.newTabSpec("Videos");
        // setting Title and Icon for the Tab
        videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
        videospec.setContent(R.id.tab2content);
    tabHost.addTab(photospec);
tabHost.addTab(videospec);
tabHost.setOnTabChangedListener(this);
        FragmentManager fm = getSupportFragmentManager();
        if (fm != null) {
            FragmentTransaction ft = fm.beginTransaction();
            Tab1Fragment1 t1f1 = new Tab1Fragment1();
            ft.add(R.id.tab1content, t1f1, "t1f1");
            addToBackStack(t1f1);
            ft.commit();
        }
    }

    public void onTabChanged(String tabId) {
        if (tabHost.getCurrentTab()==0) {
        }
        if (tabHost.getCurrentTab()==1) {
            if (alTab2.size() == 0) {
                FragmentManager fm = getSupportFragmentManager();
                if (fm != null) {
                    FragmentTransaction ft = fm.beginTransaction();
                    Tab2Fragment1 t2f1 = new Tab2Fragment1();
                    ft.add(R.id.tab2content, t2f1, "t2f1");
                    addToBackStack(t2f1);
                    ft.commit();
                }
            }
        }
    }

    public void addToBackStack(Fragment f) {
        if (tabHost.getCurrentTab()==0) {
            if (alTab1.size() > 0) {
                FragmentTransaction ft = getSupportFragmentManager()
                        .beginTransaction();
                ft.hide(alTab1.get(alTab1.size() - 1));
                ft.commit();
            }
            alTab1.add(f);
        }
        if (tabHost.getCurrentTab()==1) {
            if (alTab2.size() > 0) {
                FragmentTransaction ft = getSupportFragmentManager()
                        .beginTransaction();
                ft.hide(alTab2.get(alTab2.size() - 1));
                ft.commit();
            }
            alTab2.add(f);
        }
    }

    @Override
    public void onBackPressed() {
        if (tabHost.getCurrentTab()==0) {
            if (alTab1.size() > 1) {
                FragmentTransaction ft = getSupportFragmentManager()
                        .beginTransaction();
                ft.remove(alTab1.get(alTab1.size() - 1));
                alTab1.remove(alTab1.size() - 1);
                ft.show(alTab1.get(alTab1.size() - 1));
                ft.commit();
                return;
            }
        }
        if (tabHost.getCurrentTab()==1) {
            if (alTab2.size() > 1) {
                FragmentTransaction ft = getSupportFragmentManager()
                        .beginTransaction();
                ft.remove(alTab2.get(alTab2.size() - 1));
                alTab2.remove(alTab2.size() - 1);
                ft.show(alTab2.get(alTab2.size() - 1));
                ft.commit();
                return;
            }
        }
        super.onBackPressed();
    }

}

0 个答案:

没有答案