处理后退按钮转到HomeFragment?

时间:2016-12-07 07:20:04

标签: android

导航抽屉,处理后退按钮转到HomeFragment

这是SecurityFragment

        import android.app.FragmentTransaction;
        import android.os.Bundle;
        import android.app.Fragment;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;


        /**
         * A simple {@link Fragment} subclass.
         */
        public class SecurityFragment extends Fragment {


            public SecurityFragment() {
                // Required empty public constructor
            }



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

                // Inflate the layout for this fragment
                return inflater.inflate(R.layout.fragment_security, container, false);
            }


        }

这是HomeFragment(当onBackPressed我只是希望用户被定向到主页)当前的情况是在onBackPressed正在运行的应用程序被销毁

        package sampleapp.razen.com.sampleapp;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.NavigationView;
    import android.support.design.widget.Snackbar;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.MenuItem;
    import android.view.View;
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.widget.Toast;

    public class Menu extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu);
            android.app.Fragment fragment = new MenuFragment(); // create a fragement object
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.mainFrame, fragment);
            ft.commit();


            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {

    //this the email icon on the home page
                @Override
                public void onClick(View view) {
                    String myEmail[]={"john@balloonventures.com"};
                    Intent sendMail = new Intent(Intent.ACTION_SEND);
                    sendMail.putExtra(Intent.EXTRA_EMAIL,myEmail);
                    sendMail.putExtra(Intent.EXTRA_SUBJECT,"(Type your subject)");
                    sendMail.setType("plain/text");
                    //incase you have to add something else put here
                    sendMail.putExtra(Intent.EXTRA_TEXT,"Your phone:+2547");
                    startActivity(sendMail);


                }

            });

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    //taxi module
        public void taxi_id(View view){
            Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.Taxi.class);
            startActivity(timer);}
    //host home module
        public void hosthome_id(View view){
            Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.MainActivity.class);
            startActivity(timer);}
        //calling enterprenuers module
        public void enter_id(View view){
            Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.SectionListView.class);
            startActivity(timer);}
        //calling UKV/ICV
        public void ukvicv_id(View view){
            Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.UkvIcv_Home.class);
            startActivity(timer);}
        public void finacial_id(View view){
            Intent finacial=new Intent(this,sampleapp.razen.com.sampleapp.FinacialHomePage.class);
            startActivity(finacial);}


        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        public void displayView(int viewId) {

            Fragment fragment = null;
            String title = getString(R.string.app_name);

            switch (viewId) {
                case R.id.nav_12wks_program:
                    Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.ExpandableListMainActivity.class);
                    startActivity(timer);

                    break;


                case R.id.nav_emergency_contact:
                    Intent flow=new Intent(this,sampleapp.razen.com.sampleapp.EmergencyContact.class);
                    startActivity(flow);

                    break;


                case R.id.nav_survey:
                    fragment = new SurveyFragment();
                    title = "Survey";
                    break;

                case R.id.nav_psld:
                    fragment = new PsldFragment();
                    title = "Psld";

                    break;
                case R.id.nav_security:
                    fragment = new SecurityFragment();
                    title = "All Volunteers Mu";

                    break;
                case R.id.nav_ukv:
                    fragment = new UkvKiswaFragment();
                    title = "Ukv's kiswahili";

                    break;

                case R.id.nav_agreement:
                    fragment = new AgreementFragment();
                    title = "Agreement ";

                    break;
                case R.id.nav_njoro_geo:
                    fragment = new GeoNjoroFragment();
                    title = "Njoro Geography ";

                    break;




            }

            if (fragment != null) {
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.mainFrame, fragment);
                ft.commit();
            }

            // set the toolbar title
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle(title);
            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);

        }    @SuppressWarnings("StatementWithEmptyBody")

        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            displayView(item.getItemId());
            return true;
        }
    }

1 个答案:

答案 0 :(得分:0)

将片段transection添加到Fragment Backstack befor commit

 FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.mainFrame, fragment);
        ft.addToBackStack( "tag" );
        ft.commit();