1

I am an Android/Java novice and I have a question based on following scenario:

  1. I have BaseActivity which is extended by number of other activities, say A1, A2, and A3
  2. Activities A1, and A2 may request user to enter some Part#s. Activity A3 doesn't have this functionality as it will never ask for a Part#.
  3. Part# is entered by user in a PartDialogFragment. Since user may be prompted for a Part# from A1 and A2 activities, I am thinking to put code that instantiates and shows my PartDialogFragment in BaseActivity so I don't have to duplicate same call in Activities A1 and A2.
  4. PartDialogFragment has interface that defines onPartEntered()

Question: Should I implement the interface from PartDialogFragment in BaseActivity or in activities A1 and A2?

Basically, I am thinking to do something like this:

class PartDialogFragment extends DialogFragment{
    private PartListener mPartListener;

    public interface PartNumberListener{
        void onPartEntered(String part#);
    }
    ...
    ...

  @Override
  public void onAttach(Activity activity){
    super.onAttach(activity);
    try{
      mPartListener = (PartListener) activity;
    } catch(ClassCastException e){
      throw new ClassCastException(activity.toString() + " must implement PartListener");
    }
  }
  ...
  ...
}

public abstract class BaseActivity 
  implements PartDialogFragment.PartNumberListener {  //**should I implement it here?**

    ...
    ...

    public void ShowPartNumberDialog(){
        //creates and shows PartDialogFragment defined above
    }
}

public class A1 extends Activity
  implements PartDialogFragment.PartNumberListener {  //**or in A1 and A2?**
    ...
    ...
    if (something){
        ShowPartNumberDialog();  //this is defined in BaseActivity
    }
}

public class A1 extends Activity
  implements PartDialogFragment.PartNumberListener {  //**or in A1 and A2?**
    ...
    ...
    if (something){
        ShowPartNumberDialog();  //this is defined in BaseActivity
    }
}

public class A3 extends Activity{
    ...
    ...
    //**this will never ask for Part#, so no need to implement PartDialogFragment.PartNumberListener here.**
}
pixel
  • 9,653
  • 16
  • 82
  • 149

2 Answers2

1

You can have abstract PartActivity that will implement your onPartEntered logic. A1 and A2 will extend this PartActivity, A3 will extend BaseActivity directly, so it will not have this logic.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Thanks Gennadii but I cannot make these changes. I have to stick with the activities I have currently, that is BaseActivity, A1, A2, and A3 activities as described above. – pixel Jul 17 '15 at 22:59
  • 1
    ok.. then A1 and A2 should implement your interface. If implementation is different you just implement callbacks in activities and you are done. If it's the same, then you can put this code into, say, PartManager, and call it in callbacks like this: class A1 { ... onPartEntered() { PartManager.handlePartEntered(); } } and the same for A2. – Gennadii Saprykin Jul 17 '15 at 23:03
1

A1 and A2 activities need to implement the interface, because if the base class is abstract, then by definition you are required to create subclasses of it to instantiate. The subclasses will be required (by the compiler) to implement any interface methods that the abstract class left out.

Vinicius DSL
  • 1,839
  • 1
  • 18
  • 26
  • Thanks Vinicisu DSL. Please note, the interface is defined in the DialogFragment, not in the abstract class BaseActivity. It is there so that my DialogFragment can communicate with A1, A2 activities. I was thinking since I make call ShowPartNumberDialog() in BaseActivity (so I dont have to duplicate same method in A1 and A2), I could also centralize the interface implementation. – pixel Jul 17 '15 at 23:04
  • 1
    Yes, but you say "Should I implement the interface from PartDialogFragment in BaseActivity or in activities A1 and A2?" BaseActivity is abstract, I recommend implement interfaces on non-abstract class, see this question for more information http://stackoverflow.com/questions/197893/why-an-abstract-class-implementing-an-interface-can-miss-the-declaration-impleme – Vinicius DSL Jul 17 '15 at 23:06
  • 1
    I see and the reason I asked this question is that I am not sure how Android handles activities in this case. Reason: I create Dialog Fragment and call it from BaseActivity, but I implement listener for that dialog in activities A1 and A2. That is part that confuses me. – pixel Jul 17 '15 at 23:22
  • 1
    When you attach the listener to the fragment, you are attaching the activity context from the childs , not the BaseActivity directly, you can implement listener in each child activity and keep the call on the BaseActivity, because each instance of the child contains the base parts and methods. – Vinicius DSL Jul 17 '15 at 23:40
  • 1
    Vinicius DSL Thank you, much much appreciated! – pixel Jul 17 '15 at 23:43