0

I've a class MainActivity in which I'm implementing tabs+swipe navigation. I'm creating different java files for the different fragnents. But the problem is I won't be able to access the values of the variables in MainActivity class. Right now I've put the fragment class inside the MainActivity class and the app is working fine. How to accomplish the same result if want to create separate java file for separate fragments. This is my fagment class for now :

public static class fragmentLeft extends Fragment{
    public fragmentLeft(){} 

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_left,
                container, false);
        View.OnClickListener mStartListener = new OnClickListener() {
            public void onClick(View v) {
                //mChronometer.start();

                if (v.getId() == R.id.sign_in_button && !mPlusClient.isConnected()) {
                    Toast.makeText(getActivity(), "sadf", Toast.LENGTH_LONG).show();
                    if (mConnectionResult == null) {
                        mConnectionProgressDialog.show();
                    } else {
                        try {
                            mConnectionResult.startResolutionForResult(getActivity(), REQUEST_CODE_RESOLVE_ERR);
                        } catch (SendIntentException e) {
                            // Try connecting again.
                            Toast.makeText(getActivity(),(CharSequence) e, Toast.LENGTH_LONG).show();
                            mConnectionResult = null;
                            mPlusClient.connect();
                        }
                    }
                }

            }
        };
        rootView.findViewById(R.id.sign_in_button).setOnClickListener(mStartListener);

        return rootView;
    }
}

These are the variables which are present in the MainActivity class that I want to access from the fragment class :

private static ProgressDialog mConnectionProgressDialog;
private static PlusClient mPlusClient;
private static ConnectionResult mConnectionResult;
omerjerk
  • 4,090
  • 5
  • 40
  • 57

1 Answers1

1

Your question is very broad and it is not clear to me exactly what you are looking for. However, what you are seeking to do is relatively straightforward. Take a look at this example (which relates to Google Play Game Services and is rather more complex than what you are seeking to achieve):

Google Play Game Services Multi-Player Device Orietation change kicks user out of room

There are various ways of accessing variables in different fragments and the "recommended" way is probably to implement a listener interface. I often use a simpler approach based on calling a method in a fragment from the activity like this :

    myFrag fragment = (myFrag) getSupportFragmentManager().findFragmentByTag("myFrag");
    fragment.someMethod();

Another simple approach is to use shared preferences to access the variables directly.

Community
  • 1
  • 1
IanB
  • 3,489
  • 1
  • 20
  • 24