Call special fragment in Navigation Drawer Activity from separate Activity

113 views Asked by At

I have a little problem with my MainActivity where the Navigation Drawer(built with Android Studio) is implemented. I want to call a special fragment(not the default startup fragment) on MainActivity startup from a different activity.

As example: I want to open the MainActivity with the Fragment "CategoriesMain" from a any other activity in my application. How can I achieve this feature?

Please take a look at my MainActivity.java file:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments

    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new DashboardMain();
            break;
        case 1:
            fragment = new IncomeMain();
            break;
        case 2:
            fragment = new LossMain();
            break;
        case 3:
            fragment = new CategoriesMain();
            break;
        case 4:
            fragment = new VendorsMain();
            break;
        case 5:
            fragment = new Logout();
            break;
        default:
            break;
    }

    if (fragment != null) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, fragment).addToBackStack("fragback").commit();

    }
}

Does anybody has a suggestion for my problem? I thought about using bundles or create a SavedInstanceState of the MainActivity+CategoriesMain fragment manually in the different activity?!

Any help would be appreciated and kind regards, Christopher

0

There are 0 answers