Access getActivity () inside a static method

I have this class that calls the setPoint method

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 that after I press the button to add a new Serial list to the List, I can return to the previous menu from

R.layout.point_creation to R.layout.listpoints

To navigate fragments, I usually 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 get around it by making the static class non-static? I have some global flags that I use in static classes (there are 2 of them) which will be a little painful to export since

public class PointCreation(int something) extends Fragment 

- this is what I can not do.

+5
source share
3 answers

:

Activity activity = (Activity)view.getContext()

FragmentActivity (, ), Context to FragmentActivity ( Activity), getSupportFragmentManager()

FragmentActivity activity = (FragmentActivity)view.getContext();
FragmentManager manager = activity.getSupportFragmentManager();
+18

. , , - singleton . , singleton snippet:

    static PointsList instance;

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

onCreate :

instance = this;

static setPoint. , PointsList.getInstance().setPoint();

p.s. ? static , singleton .

0

You can use the code below;

private static FragmentActivity myContext;

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

You can use myContextasContext

0
source

Source: https://habr.com/ru/post/1535947/


All Articles