0

I get data from Activity in method called onConnected,Im get current location:longitude,latitude.How can I put this data to fragment?Yes,I know about Bundle and how deliver data trough that way,but I cant create Bundle in onConnected method and setArguments to fragment,because I created fragment in onCreate Activity method.I created class variable longitude and latitude,but in onCreate that variables = 0.How can I send data to fragment?I need to get current location in Activity and send it to fragment.

Activity:

public class ScreenForTagging extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
double latitude,longitude;
    Location mLastLocation;
GoogleApiClient mGoogleApiClient;
    Bundle bundlelocation;
    Tagging_screen tagging_screen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_for_tagging);
        tagging_screen = new Tagging_screen();
        bundlelocation = new Bundle();

        bundlelocation.putDouble("latitude",latitude);
        bundlelocation.putDouble("longitude", longitude);

        tagging_screen.setArguments(bundlelocation);

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragmentCont, tagging_screen);
        fragmentTransaction.commit();
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
mGoogleApiClient.connect();

    }

    @Override
    public void onConnected(Bundle bundle) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            latitude = mLastLocation.getLatitude();
            longitude = mLastLocation.getLongitude();

            Toast.makeText(ScreenForTagging.this, "latitude:"+latitude+"longitude"+longitude, Toast.LENGTH_SHORT).show();
        }

    }


    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
    public void onStop(){
        super.onStop();
        mGoogleApiClient.disconnect();

    }

}

Fragment:

class Tagging_screen extends Fragment {
    Location mLastLocation;
    String URL;
    Button btndownload;
    ImageView imageView;
    EditText editText;
    Button btngallery;
    FloatingActionButton floatbtn;
    LocationManager locationManager;
    GoogleApiClient mGoogleApiClient;
    double latitude, longitude;
    private static final int RESULT_LOAD_IMAGE = 1;
Context mContext;
    // private static final int RESULT_OK = 2;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tagging_screen, null);
        imageView = (ImageView) v.findViewById(R.id.imageView);
        editText = (EditText) v.findViewById(R.id.editText_download);
        btndownload = (Button) v.findViewById(R.id.btn_download);
        btngallery = (Button) v.findViewById(R.id.button_gallery);
        floatbtn = (FloatingActionButton) v.findViewById(R.id.floatbutton);
        locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
mContext = getActivity();
        return v;

    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        latitude = getArguments().getDouble("latitude");
   //     longitude = getArguments().getDouble("longitude");


        floatbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });


        btndownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                URL = editText.getText().toString();
                Picasso.with(getActivity()).load(URL).into(imageView);
            }
        });
        btngallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getActivity().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);

            assert cursor != null;
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            Log.d("ActivityResult", "OnActivityResult");
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }




}
Rost
  • 53
  • 8

1 Answers1

1

You already have a reference to the fragment in your activity (Tagging_screen tagging_screen;), so declare a public method in your Fragment class Tagging_Screen as shown below and then call tagging_screen.setLatAndLongt(lat, longt) from your activity.

Add below method inside Tagging_Screen.java

public void setLatAndLongt(double lat, double longt){
    latitude = lat;
    longitude = longt;
}
Guru_VL
  • 402
  • 3
  • 6
  • how can I use this values?I try to make Toast in OnActivityCreated in Fragment but both values are 0. – Rost Feb 10 '16 at 14:50
  • onConnected started after onCreate method in Activity,where I create a fragment.In onCreate values are 0.And in fragment they are 0.How fix that? – Rost Feb 10 '16 at 15:44