Fragments disappears after resuming

允我心安 提交于 2020-01-07 02:18:28

问题


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.

 }

回答1:


ft.show(frag1);

should be

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



来源:https://stackoverflow.com/questions/17160836/fragments-disappears-after-resuming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!