Search code examples
javaandroidandroid-fragmentsnavigation-drawerandroid-tabhost

Fragment is not working properly


I am using navigation drawer in my app,everything working fine,the issue is onbackpress my app getting close,there is no crash of app,just my app is closing

Following is my code and screen shot ,can anyone help?

MainActivity

@Override
    public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    private void displayView(int position) {
        Fragment fragment = null;
        String title = getString(R.string.app_name);

        switch (position) {
            case 0:
                fragment = new HomeFragment();

                title = getString(R.string.title_home);
                break;
            case 1:
                fragment = new FriendsFragment();


                title = getString(R.string.title_friends);
                break;
            case 2:
                fragment = new MessagesFragment();


                title = getString(R.string.title_messages);
                break;

            case 3:
                fragment = new Profiles();


                title = getString(R.string.title_pro);
                break;
            case 4:
                fragment = new AboutUsFragment();
                title = getString(R.string.title_aboutus);
                break;
            case 5:
                fragment = new FAQ();
                title = getString(R.string.title_faq);
                break;
            case 6:
                fragment = new Privacypolicy();
                title = getString(R.string.title_privacypolicy);
                break;
            case 7:
                fragment = new ContactUs();
                title = getString(R.string.title_contacts);
                break;

            case 8:
                // session.logoutUser();
                LoginManager.getInstance().logOut();
               // Intent intent=new Intent(MainActivity.this,LoginPage.class);
                // startActivity(intent);
                title = getString(R.string.title_logout);
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.commit();
            getSupportActionBar().setTitle(title);
        }
    }

   /* @Override
    public void onBackPressed() {

        int count = getFragmentManager().getBackStackEntryCount();

        if (count == 0) {
            super.onBackPressed();
            //additional code
        } else {
            getFragmentManager().popBackStack();
        }

    }*/

    @Override
    public void onBackPressed(){
        FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 1) {
            Log.i("MainActivity", "popping backstack");
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        } else {
            Log.i("MainActivity", "nothing on backstack, calling super");
            super.onBackPressed();
        }
    }
}

HomeFragment.java

public class HomeFragment extends Fragment {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    private FragmentTabHost tabHost;
    private SearchView searchView;
    // private String strtext;
    // private String FBIDs;
    // private String GIDs;


    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        tabHost = new FragmentTabHost(getActivity());
        tabHost.setup(getActivity(), getChildFragmentManager(), R.layout.my_parent_fragment);
        // strtext = getArguments().getString("user_login_id");


        //  System.out.println("<<<<<<<<<<<<<< Session ID :  " + strtext);

        Bundle arg1 = new Bundle();
        arg1.putInt("Arg for Frag1", 1);
       /* if(strtext!=null) {
            arg1.putString("user_logid", strtext);
        }*/

        tabHost.addTab(tabHost.newTabSpec("one").setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_profile_tabs, "Home")), DiscoverFragment.class, arg1);
        Bundle arg2 = new Bundle();

        arg2.putInt("Arg for Frag2", 2);

       /* if(strtext!=null) {
            arg2.putString("user_logid_sectab", strtext);
        }*/

        tabHost.addTab(tabHost.newTabSpec("Sec").
                setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_frnds_tab, "Invite")), ShopFragment.class, arg2);


        Bundle arg3 = new Bundle();
        arg3.putInt("Arg for Frag3", 3);

      /*  if(strtext!=null) {
            arg3.putString("user_logid_thirdtab", strtext);
        }*/

        tabHost.addTab(tabHost.newTabSpec("Third").
                setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_wish_tab, "Wish")), Thirdtab.class, arg3);


        Bundle arg4 = new Bundle();
        arg4.putInt("Arg for Frag4", 4);
        /*if(strtext!=null) {
            arg4.putString("user_logid_fourthtab", strtext);
        }*/

        tabHost.addTab(tabHost.newTabSpec("Four").
                setIndicator(getTabIndicator(tabHost.getContext(), R.drawable.icon_notification_tab, "Alert")), FourthTabs.class, arg4);

        return tabHost;


    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public View getTabIndicator(Context context, int icon, String text) {
        View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
        ImageView iv = (ImageView) view.findViewById(R.id.indicatorImageView);
        iv.setImageResource(icon);
        TextView txt = (TextView) view.findViewById(R.id.txttb);
        txt.setText(text);
        return view;
    }


}

DiscoverFragment.java

public class DiscoverFragment extends Fragment {


    private Button btn;

    public DiscoverFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.discover_frag, container, false);

        btn=(Button)rootView.findViewById(R.id.btnhomf);

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                WishaFriend ff = new WishaFriend();

                android.support.v4.app.FragmentManager fm = getActivity().getSupportFragmentManager();
                android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
                fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
               // ft.replace(R.id.container_body,ff);
               ft.replace(((ViewGroup) getView().getParent()).getId(), ff);

                // fm.addToBackStack("sds");
               ft.commit();
            }
        });

        return rootView;
    }

}

when i run app it shows this as first screen

enter image description here

then i click on gotonextfrag button,and it will redirect to next frag and i am getting this screen

enter image description here

now from this fragment if i press backbutton of device my app get close

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">



        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/MyToolBarStyle.Base"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"

                android:src="@drawable/toolbartitle"/>

        </android.support.v7.widget.Toolbar>


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <!-- activity view -->
        <!-- Framelayout to display Fragments -->
        <FrameLayout
            android:id="@+id/container_body"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- navigation drawer -->
        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:name="com.balloons.balloonationn.activity.FragmentDrawer"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

Solution

  • It's the normal behavior of Activity. When you press back button, it finish the current activity.

    Override back button