简体   繁体   中英

Access var from MainActivity in another Extended class

So I need to have access to a variable from MainActivty from another class. what's the best way to do so. Below is the scenario but I cannot call frag.receiveInt(give_this_int_to_fragment);

MainActivity.java:

    import com.me.fragments.FragmentExtender

    public class MainActivity{
        int give_this_int_to_fragment;

        protected void onCreate(...){
            Fragment frag = new FragmentExtender();
            give_this_int_to_frag = new int();
            frag.receiveInt(give_this_int_to_frag);
        }
    }

FragmentExtender.java:

    public class FragmentExtender extends Fragment{
         int receive_int_from_main;

         public View onCreateView(...){...}

         receiveInt(int_from_main){
              receive_int_from_main = int_from_main;
         }
    }

I'm not trying to create a duplicate variable, just a pointer to that variable in MainActivity if you catch my drift.

Create getter and setter for that variable in Activity

public class MainActivity{
    int give_this_int_to_fragment;

    protected void onCreate(...){
        Fragment frag = new FragmentExtender();

    }
}

public int getgive_this_int_to_fragment(){
     return give_this_int_to_fragment;
}
public void getgive_this_int_to_fragment(int var){
     give_this_int_to_fragment = var;
}

Now, in fragment you can use getActivity() to get activity context and then call getter

public class FragmentExtender extends Fragment{


     public View onCreateView(...){
         //....
         int var = ((MainActivity) getActivity()).getgive_this_int_to_fragment();

     }
}

The more elegant way will be to implement an interface in the Fragment and make the app inherit from it. That way you can comunicate that way.

interface FragmentInterface{
    Object getMainValue();
    void passValueToMain(Object obj);
}

With this code, you just have to add it to the MainActivity declaration and in the fragment constructor

public class MainActivity implements FragmentInterface{
    int give_this_int_to_fragment;

    protected void onCreate(...){
        Fragment frag = new FragmentExtender();
        ....
    }
}

And then in the Fragment

public class FragmentExtender extends Fragment{
    private FragmentInterface mInterface;
    int receive_int_from_main;

    public View onCreateView(...){...}

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setListeners(context);
    }

    //This is for pre Marshmallow versions
    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            setListeners(activity);
        }
    }

    private void setListeners(Object object) {
        if (object instanceof FragmentInterface) {
            mListener = (FragmentInterface) object;
        } else {
            throw new RuntimeException(object.toString()
                + " must implement FragmentInterface");
        }
    }
}

This will work for any fragment and any activity that implement them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM