简体   繁体   中英

how to call an activity from a self created slide menu (one which found in default in navigation drawer activity) under Tabbed activity

I created a slide menu (one which found in default in navigation drawer activity) under a tabbed activity. I can call a fragment from the slide menu choices like shown in the code below. My question is , how can I call an activity instead of a fragment. Can anyone help me on this? Here is the code

public class MainActivity extends AppCompatActivity

{
    DrawerLayout drawerLayout;
    NavigationView navigationView;
    android.support.v4.app.FragmentManager FM;
    FragmentTransaction FT;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        navigationView = (NavigationView) findViewById(R.id.shitstuff);

        FM = getSupportFragmentManager();
        FT = FM.beginTransaction();
        FT.replace(R.id.containerView, new TabFragment()).commit();

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                // Handle navigation view item clicks here.
                int id = item.getItemId();

                if (id == R.id.nav_history) {
                    FragmentTransaction fragmentTransaction = FM.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView, new HistoryFragment()).commit();

                }
                else if (id == R.id.nav_gallery) {
                    FragmentTransaction fragmentTransaction = FM.beginTransaction();
                    fragmentTransaction.replace(R.id.containerView, new GalaryFragment()).commit();

                }

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });
        android.support.v7.widget.Toolbar toolbar =(Toolbar)findViewById(R.id.toolbar);
        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
    }


}

Simply call activity instead of a fragment. If you are new to the Android, on below code "this" refers to a current activity, HistoryAcitvity is a new activity which is going to start. Before you write this code, create activity, and add that activity to manifest(if you do it in the android studio it does automatically).

if (id == R.id.nav_history) { Intent historyActivity = new Intent(this ,HistoryAcitivity.class); startActivity(historyActivity);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM