7

I have this class here which calls the method setPoint

public class PointsList extends Fragment {    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.listpoints, container, false);

    public static class PointCreation extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                         Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.point_creation, container, false);
        setPoint(view, CREATE);
        return view;
    }
}

static final void setPoint(View view, int goal) {
final EditText SerialField = (EditText) view.findViewById(R.id.Serial);
    if(goal == CREATE) {
        Button buttonGuardar = (Button) view.findViewById(R.id.buttonGuardar);
        buttonGuardar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String Serial = SerialField.getText().toString();
                pointsList.add(new Serial);
                //go back to R.layout.listpoints
            }
        });
    }
}

My goal is after I click the button to add the new Serial to the List, I can go back to the previous menu from

R.layout.point_creation to R.layout.listpoints

To move around fragments I generally use something like this:

            Fragment fragment = new PointsList();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();

But inside:

static final void setPoint(View view, int goal)
getActivity().getSupportFragmentManager();

cannot be referenced from a static context, and I don't know how to go around it with making the static class non-static? I've some global flags which I use in the static Classes (have 2 of them) that would be a bit painfull to export since

public class PointCreation(int something) extends Fragment 

is something I can't do.

heisenberg
  • 1,172
  • 6
  • 14
  • 31
  • 1
    Either make your method non-static, pass it an Activity or Context or whatever it needs as a parameter, or have a `singleton` instance statically cached which you can obtain as needed. But remember that you not only have to have the object, you have to be calling it is method at an actually appropriate time, possibly from an appropriate thread, etc. – Chris Stratton Apr 10 '14 at 14:23
  • already made everything non static, it's now working like a charm – heisenberg Apr 10 '14 at 14:46

3 Answers3

22

You can get the activity from view:

Activity activity = (Activity)view.getContext()

If you use FragmentActivity (it seems to be so), then cast Context to FragmentActivity (instead of regular Activity) and further you will able to call getSupportFragmentManager()

FragmentActivity activity = (FragmentActivity)view.getContext();
FragmentManager manager = activity.getSupportFragmentManager();
Vladimir Petrakovich
  • 4,184
  • 1
  • 30
  • 46
  • then acitivity.?? activity Fragment fragment = new PointsList(); Activity activity = (Activity)view.getContext(); FragmentManager fragmentManager = activity.getFragmentManager(); <- does not work – heisenberg Apr 10 '14 at 14:28
  • you can't call context from static method. It doesn't matter how you will get that context. You just can't do that – OFFmind Apr 10 '14 at 14:31
  • @OFFmind Why I can't use context from view, which was passed to the method? – Vladimir Petrakovich Apr 10 '14 at 14:40
  • If you pass it directly to that method - sure you can. I mean you can't get it from static method. However, that is good practice for Utils classes, that have just static methods, because you don't need to have an instances of such classes. In your case - your fragment will be created in any case. So using singleton is preferable. – OFFmind Apr 10 '14 at 14:55
  • It helped me ,Thanks :) – Farhad Sep 23 '16 at 19:38
1

You can use by below code;

private static FragmentActivity myContext;

@Override
public void onAttach(Activity activity) {
    myContext = (FragmentActivity) activity;
    super.onAttach(activity);
}

You can use myContext as Context

Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

You can't reference from a static to non-static objects. First thing, that comes to mind is to use singleton pattern for your fragment. In other words add to you fragment singleton snippet:

    static PointsList instance;

    public PointsList getInstace(){
     if(instance == null){
        instance = new PointsList ();
     }
    return instance;
    }

and in your fragment onCreate method assign it to the instance:

instance = this;

after that you can remove static modifier from setPoint method. And call it from any part of your project like PointsList.getInstance().setPoint();

p.s. what goals from static you have to use? You should use static very carefully, many things can be done through singleton instead of using statics.

OFFmind
  • 597
  • 1
  • 5
  • 13