Henrik Bolding Frank
Henrik Bolding Frank

Reputation: 23

Calling fragment method without instantiating actual fragment

I am not a very advanced user in android, and i don't know if this is the optimal way, but i have a problem with my structure. When doing webapps and desktop apps, i have been used to separate logic and presentation from each other, and i try to do this in android.

So far i have the logic for each "view" inside each fragment, so i want to have a onClick function for each button in each activity (as this is recommended by google), to only make a method call into each fragment, and handle the logic from there.. The problem is when i use the navigation drawer, and the recommended implementation of interfaces, i am not able to reach the functions, as i am not implementing the class, but working towards the fragment interface.

So far i have this for handling menu clicks and changing fragments:

private void displayFragment(int menuId) {
    Fragment fragment = null;

    switch (menuId) {
        case R.id.nav_search:
            fragment = new SearchFragment();
            break;
        case R.id.nav_create_ticket:
            break;
        case R.id.nav_logout:
            LogoutDialogFragment logoutDialogFragment = new LogoutDialogFragment();
            logoutDialogFragment.show(getSupportFragmentManager(), "Logud");
            break;
    }

    //replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_fragment_container, fragment);
        ft.commit();
    }

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

So of course i am only able to call methods implemented from the interface, and not the public functions of my "searchfragment" class.. I don't think this is the right way, but what i want to achieve is something like this:

public void searchBtnClicked(View view) {
    fragment.searchForLicensePlate();
}

Is there a better way? I don't think i should instantiate a searchfragment inside the searchBtnClicked method, as the fragment already has been started in the displayFragment.

Thanks

Upvotes: 1

Views: 73

Answers (1)

cutiko
cutiko

Reputation: 10527

1.- The drawer (NavigationView) is a ViewGroup, you can add a Fragment inside of it and then do whatever you want.

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@drawable/side_nav_bar">

        <fragment
            android:id="@+id/drawerFragment"
            android:name="cl.cutiko.someapp.views.main.drawer.DrawerFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

</android.support.design.widget.NavigationView>

2.- If you want to use the default implementation then you don't need to have each function in a fragment, you can use a Presenter and return the result of the presenter to the View. See MVP design pattern here

Upvotes: 1

Related Questions