Search code examples
androidandroid-activityfragment

Implements inner Fragment class in parent Activity


I have the following code:

public class SettingsActivity extends AppCompatActivity implements SettingsActivity.SettingsFragment.SendToActivity {
  ...
  public static class SettingsFragment extends PreferenceFragmentCompat {
    
    SendToActivity callback;

    public void setSendToActivity (SendToActivity callback) {
      this.callback = callback;
    }

    public interface SendToActivity {
      public void send(int result);
    }
    ...
  }

  @Override
  public void onAttachFragment(Fragment fragment) {
    if (fragment instanceof SettingsFragment) {
      SettingsFragment settingsFragment = (SettingsFragment) fragment;
      settingsFragment.setSendToActivity(this);
    }
  }

  public void send(int result) {
    ...
  }
}

But I get the following error: cyclic inheritance involving SettingsActivity in the the declaration of the SettingsActivity class. What I'm doing wrong?


Solution

  • I answer myself: The fragment class needs to be on its own java file. Then just implement it normally (implements SettingsFragment.SendToActivity).