GDanger
GDanger

Reputation: 1671

Android static inner fragments and interfaces

From reading the Android docs http://developer.android.com/guide/components/fragments.html it seems that fragments should be a static inner class. It also seems that they should use an interface to communicate with activities. When I try to do both I get a cyclic dependency.

public class MyActivity extends FragmentActivity implements MyFragment.Listener{
    // Activity methods

    @Override
    public void foo(){

    }

    static class MyFragment extends Fragment{
    interface Listener{
        public void foo();
    }
    // Fragment Methods
    }

}

Gives me a cyclic dependency error. What is the correct design to use?

Upvotes: 0

Views: 491

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

If the fragment is already an inner class then there is no need for the fragment to implement the interface just to communicate with the activity. You can directly communicate with your MyActivity inside the Fragment if it is an inner class.

If it is a separate class then you can implement an interface to communicated with the activity.

Upvotes: 1

Related Questions