0

Learning how to use fragments in android. I'm following documentation on Android: https://developer.android.com/training/basics/fragments/communicating.html

According to documentation I'm suppose to use static to implement my fragment interface in my activity.

However I'm getting error. "modifier static not allowed"

My host activity:

public static class RecordWorkoutActivity extends AppCompatActivity
    implements WorkoutFragment.OnFragmentInteractionListener{

My fragment:

public class WorkoutFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

Ultimately I need my activity to display one fragment if on landscape orientation and a different fragment if on Portrait but for know I just need to be able to connect my fragments with my Activity.

user2300867
  • 593
  • 1
  • 12
  • 28

2 Answers2

1

The documentation most probably includes static in public static class because the classes in examples there are inner classes.

If you implement your activity not as an inner class, you don't need the static modifier. Just remove it.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • I doubt it, they have `MainActivity` class as static, which is wrong in all possible ways. Probably just a typo. – Sergey Emeliyanov Mar 26 '18 at 20:38
  • If I remove static I get. "Class must be declared abstract or implement abstract method OnFragmentIteraction – user2300867 Mar 26 '18 at 20:40
  • @user2300867 Well, you do need to actually implement the method. – lexicore Mar 26 '18 at 20:41
  • Makes sense. I was just trying to stick with the documentation. Thanks. Also this was very helpful. https://stackoverflow.com/questions/24777985/how-to-implement-onfragmentinteractionlistener?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – user2300867 Mar 26 '18 at 20:54
1

Not sure why that's part of the documentation, because as explained in this article, static classes can only exist nested in other classes. Have you tried removing "static" and running the code?

Chris
  • 909
  • 8
  • 18