片段恢复后消失 [英] Fragments disappears after resuming

查看:143
本文介绍了片段恢复后消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有actionbarsherlok tabs.Three片段放置这些tabs.After pressing home键内的主要活动,我从后台立即恢复应用程序中显示的所有片段没有任何问题。但是,当我尝试经过一段时间恢复,碎片被消失了。我只能看到标签header.For澄清,我提供code为主要活动,

 保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        的setContentView(R.layout.activity_main);
        动作条酒吧= getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        //绘制对象D = getResources()getDrawable(R.drawable.tab_bg)。
        // bar.setBackgroundDrawable(D);        ActionBar.Tab TAB1 = bar.newTab();
        ActionBar.Tab TAB2 = bar.newTab();
        ActionBar.Tab TAB3 = bar.newTab();
        tab1.setText(假日);
        // tab1.setIcon(R.drawable.abs__ic_menu_share_holo_dark);
        tab2.setText(旅行者的故事);
        // tab2.setIcon(R.drawable.abs__ic_voice_search);
        tab3.setText(时刻);
        // tab3.setIcon(R.drawable.abs__ic_cab_done_holo_dark);        tab1.setTabListener(新MyTabListener());
        tab2.setTabListener(新MyTabListener());
        tab3.setTabListener(新MyTabListener());
        bar.addTab(TAB1);
        bar.addTab(TAB2);
        bar.addTab(TAB3);        如果(Utils.isNetworkAvailable(MainActivity.this)){            新GetMainCategory(MainActivity.this).execute();
        }        其他{
            AlertDialog alertDialog =新AlertDialog.Builder(MainActivity.this)
                    。创建();            alertDialog.setMessage(无网络连接!);
            alertDialog.setButton(OK,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释它){
                    // TODO添加您的code这里的按钮。
                    完();                }
            });
            alertDialog.show();
        }    }    私有类MyTabListener实现ActionBar.TabListener {
        @覆盖
        公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){            开关(tab.getPosition()){
            情况下0:                如果(frag1 == NULL){
                    //如果没有,实例化并把它添加到活动
                    frag1 = Fragment.instantiate(getApplicationContext(),
                            PackagesFragment.class.getName());
                    ft.add(android.R.id.content,frag1,饲料);
                }其他{
                    //如果它存在,只需将其固定,以显示它
                    ft.show(frag1);
                }
                返回;            情况1:                如果(frag2 == NULL){
                    //如果没有,实例化并把它添加到活动
                    frag2 = Fragment.instantiate(getApplicationContext(),
                            BlogsFragment.class.getName());
                    ft.add(android.R.id.content,frag2,档案);
                }其他{
                    //如果它存在,只需将其固定,以显示它
                    ft.show(frag2);
                }
                返回;
            案例2:                如果(frag3 == NULL){
                    //如果没有,实例化并把它添加到活动                    frag3 = Fragment.instantiate(getApplicationContext(),
                            GalleryFragment.class.getName());
                    ft.add(android.R.id.content,frag3,画廊);
                }其他{
                    //如果它存在,只需将其固定,以显示它
                    ft.show(frag3);
                }
                返回;            }        }        @覆盖
        公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){
            // TODO自动生成方法存根            //分离片段,因为另一个被附
            开关(tab.getPosition()){
            情况下0:
                ft.hide(frag1);
                返回;
            情况1:
                ft.hide(frag2);
                返回;
            案例2:
                ft.hide(frag3);
                返回;            }        }        @覆盖
        公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){
            // TODO自动生成方法存根
        }
    }//这里我有一个异步任务。 }


解决方案

ft.show(frag1);

ft.attach(frag1); 如果你下面的这个

I have a main activity which has actionbarsherlok tabs.Three fragments are placed inside these tabs.After pressing home button and I resume the application immediately from background all fragments are shown without any problem.But, when I try to resume after some time, fragments gets disappeared. I can only see the tab header.For clarification, I am providing the code for main activity,

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);


        ActionBar bar = getSupportActionBar();


        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Drawable d=getResources().getDrawable(R.drawable.tab_bg);
        // bar.setBackgroundDrawable(d);

        ActionBar.Tab tab1 = bar.newTab();
        ActionBar.Tab tab2 = bar.newTab();
        ActionBar.Tab tab3 = bar.newTab();
        tab1.setText("Holidays");
        // tab1.setIcon(R.drawable.abs__ic_menu_share_holo_dark);
        tab2.setText("Traveller's Tales");
        // tab2.setIcon(R.drawable.abs__ic_voice_search);
        tab3.setText("Moments");
        // tab3.setIcon(R.drawable.abs__ic_cab_done_holo_dark);

        tab1.setTabListener(new MyTabListener());
        tab2.setTabListener(new MyTabListener());
        tab3.setTabListener(new MyTabListener());
        bar.addTab(tab1);
        bar.addTab(tab2);
        bar.addTab(tab3);

        if (Utils.isNetworkAvailable(MainActivity.this)) {

            new GetMainCategory(MainActivity.this).execute();
        }

        else {
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                    .create();

            alertDialog.setMessage("No network connection!");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Add your code for the button here.
                    finish();

                }
            });
            alertDialog.show();
        }

    }

    private class MyTabListener implements ActionBar.TabListener {
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {

            switch (tab.getPosition()) {
            case 0:

                if (frag1 == null) {
                    // If not, instantiate and add it to the activity
                    frag1 = Fragment.instantiate(getApplicationContext(),
                            PackagesFragment.class.getName());
                    ft.add(android.R.id.content, frag1, "Feeds");
                } else {
                    // If it exists, simply attach it in order to show it
                    ft.show(frag1);
                }
                return;

            case 1:

                if (frag2 == null) {
                    // If not, instantiate and add it to the activity
                    frag2 = Fragment.instantiate(getApplicationContext(),
                            BlogsFragment.class.getName());
                    ft.add(android.R.id.content, frag2, "Profile");
                } else {
                    // If it exists, simply attach it in order to show it
                    ft.show(frag2);
                }
                return;
            case 2:

                if (frag3 == null) {
                    // If not, instantiate and add it to the activity

                    frag3 = Fragment.instantiate(getApplicationContext(),
                            GalleryFragment.class.getName());
                    ft.add(android.R.id.content, frag3, "Gallery");
                } else {
                    // If it exists, simply attach it in order to show it
                    ft.show(frag3);
                }
                return;

            }

        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

            // Detach the fragment, because another one is being attached
            switch (tab.getPosition()) {
            case 0:
                ft.hide(frag1);
                return;
            case 1:
                ft.hide(frag2);
                return;
            case 2:
                ft.hide(frag3);
                return;

            }

        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }
    }

//Here I have an async task.

 }

解决方案

ft.show(frag1);

should be

ft.attach(frag1); etc. if you're following this.

这篇关于片段恢复后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆