Skip to content Skip to sidebar Skip to footer

Android: Implement Inner Class In Outer Class; Callback From Inner Class And Outer Class

I have multiple fragments in one activity that should be able to pass data inbetween them. I have used tutorials to implement callbacks. My MainActivity is the outer class in which

Solution 1:

You can't implement an inner-interface or extend an inner-class. Simply move ConnectionFragment to its own file.

This is because at compile time, these inner classes are dependent on the parent class - and NEVER the other way around. As proof, if you look at the compiled .class files, these inner-Objects are compiled as MainActivity$ConnectionFragment.class or something there-abouts. If, however, ConnectionFragment is compiled into its own file (ConnectionFragment.cass), then MainActivity.class can depend on it, and Eclipse will automatically handle the build-order.

Solution 2:

Fragments should never directly interact with each other since this creates undesired coupling.

Your fragment should call the interface method implemented by the Activity to communicate with the Activity, then the Activity can communicate/relay that interaction with the other fragment.

Solution 3:

It's seem that you can't use an interface before you declare it.

Post a Comment for "Android: Implement Inner Class In Outer Class; Callback From Inner Class And Outer Class"