Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access getActivity() inside static method

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.

like image 734
heisenberg Avatar asked Apr 10 '14 13:04

heisenberg


2 Answers

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();
like image 69
Vladimir Petrakovich Avatar answered Oct 22 '22 12:10

Vladimir Petrakovich


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

like image 32
Vishal Vaishnav Avatar answered Oct 22 '22 12:10

Vishal Vaishnav