0

I have 2 different activities that can "instantiate" the same fragment. Inside the fragment I need to save a reference to the container activity.

MyActivity1 extends FragmentActivity
MyActivity2 extends FragmentActivity

The reference is stored inside a Fragment class field. I cannot be generic:

private Activity activity;

and then:

activity = getActivity();

This way I cannot use methods implemented for custom MyActivity1/MyActivity2. Downcasting doesn't seem to help as well:

if (getActivity() instanceof MyActivity1) {
    activity = (MyActivity1) getActivity();
    activity.mMethod1(); // NOPE
} else {
    activity = (MyActivity2) getActivity();
    activity.mMethod2(); // NOPE
}

Should I use two field (one per activity type) and leave one blank? Any java smarter way to do this? Hope my question is clear.

Jumpa
  • 4,319
  • 11
  • 52
  • 100

2 Answers2

1

it does not help because you are assigning the casting to the generic type not to the specific

if (getActivity() instanceof MyActivity1) {
    ((MyActivity1) getActivity()).mMethod1(); 
} else if (getActivity() instanceof MyActivity2) {
    ((MyActivity2) getActivity()).mMethod2();  
}

I would suggest you to have an abstract class with an abstract method, and two concrete extensions of this class, that provide their own implementation of the abstract method. In this case you can keep a reference to the abstract class, and the method call will be forwarded to the correct concrete instance

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Yeah but I need a reference for further use inside other methods of the class, so I need two fields? – Jumpa Oct 17 '14 at 12:34
  • 1
    I see, this way you have always to check and cast to the exact type, that it is not that beautiful. If I were I would have an abstract MyActivity class, with the abstract an method. MyActivity1 and MyActivity2 extends MyActivity and provide the implementation of the abstract method. So you can keep a reference to MyActivity, and the correct method will be called – Blackbelt Oct 17 '14 at 12:38
  • Ok, mmm I have multiple different methods in the 2 activities, and some of them are not implemented in the other activity. – Jumpa Oct 17 '14 at 12:46
  • 1
    that's up to view. You can have also an empty method. It is not the nicer thing in the word, I know, but for few methods.. All in all I can not tell you what is the best way to design your app :) – Blackbelt Oct 17 '14 at 12:50
1

Since e.g. only MyActivity1 has mMethod1(), you'd need to cast activity every time you call it to the right class. IMHO the approach with two fields sounds better.

Code could be something like:

if (getActivity() instanceof MyActivity1) {
    activity = (MyActivity1) getActivity();
    ((MyActivity1) activity).mMethod1();
} else {
    activity = (MyActivity2) getActivity();
    ((MyActivity2) activity).mMethod2();
}

Alternatively, you can declare an interface defining a mMethod() and have both activities implement that interface.. Then you can have:

interface MyActivityInterface {
    public void mMethod();
}

and

MyActivityInterface activity;

// ...

if (getActivity() instanceof MyActivity1) {
    activity = (MyActivityIntercface) getActivity();
} else {
    activity = (MyActivityIntercface) getActivity();
}

activity.mMethod();

hth,

Laur Ivan
  • 4,117
  • 3
  • 38
  • 62