質問

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 my fragment classes are. Furthermore I have a FragmentPagerAdapter that handles the fragment transitions. Well the thing is, Eclipse wont let me implement my callback interface, that is included in one of the Fragments, for my MainActivity outer class.

This is the structure of my code:

public class MainActivity extends FragmentActivity **implements ConnectionFragment.dataCallback**{ 
//compiler error here:"ConnectionFragment cannot be resolved to a type" 
//when i leave this out i get runtime error: "MainActivity java must 
//implement dataCallback"

...

    public class SectionsPagerAdapter extends FragmentPagerAdapter  implements ConnectionFragment.dataCallback{
        @Override
        public void updateLog(View v, String line) {
            DataFragment dataFrag = (DataFragment)getSupportFragmentManager().findFragmentByTag(DataFragment.class.getName());
                if (dataFrag != null){
                    dataFrag.updateLog(v,line);
                }
        }
    ...

    }
    public static class ConnectionFragment extends Fragment {
    ...
        public interface dataCallback{
        public void updateLog(View v, String line);
        }
        dataCallback mCallback;
        private static dataCallback dummyCallback=new dataCallback(){
             @Override
             public void updateLog(View v, String line){    
             }
        };

        @Override
        public void onAttach(Activity activity){
            super.onAttach(activity);
            try {
                mCallback = (dataCallback)activity;
            } catch (ClassCastException e){
            throw new ClassCastException(activity.toString() + " must implement dataCallback");
         }
    }
    }
    public static class DataFragment extends Fragment implements ConnectionFragment.dataCallback{
        public void updateLog(View v,String line){

             TextView logTextView=(TextView)v.findViewById(R.id.log_view);
             logTextView.append("\n"+line);
    }

    ...
    }
    public static class GraphFragment extends Fragment {
    ...
    }
}

ConnectionFragment should be able to send data to DataFragment.

I appreciate your help!

役に立ちましたか?

解決

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.

他のヒント

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.

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top