title: How can I access main activity to get latitude and longitude in order to change url, and how can I change the url's languages automatically,
body: <p>I am doing a toilet android app which get the closest toilet, but I face problems.
  I want to get <code>mainactivity</code>'s latitude and longitude variable in order to change the url's latitude and longitude information which is in <code>main2activty</code>. How can I pass the value between different activity?
  Thank you for your help. Below is my, <code>mainactivity</code> and <code>main2activity</code> java code.</p>
  
  <p>This is my <code>mainactivty</code></p>
  
  <pre><code>package hk.android.1;
  
  import android.Manifest;
  import android.annotation.SuppressLint;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.support.annotation.NonNull;
  import android.support.design.widget.Snackbar;
  import android.support.v4.app.ActivityCompat;
  import android.support.v7.app.AppCompatActivity;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  import com.google.android.gms.location.FusedLocationProviderClient;
  import com.google.android.gms.location.LocationServices;
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  
  import java.util.Locale;
  
  public class MainActivity extends AppCompatActivity {
      private static final String TAG1 = MainActivity.class.getSimpleName();
      private Button button3;
      private Button button4;
      private Button button5;
      private Button button6;
      public static double latitude;
      public static double longitube;
      private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
      private FusedLocationProviderClient mFusedLocationClient;
  
      protected Location mLastLocation;
  
      private String mLatitudeLabel;
      private String mLongitudeLabel;
      private TextView mLatitudeText;
      private TextView mLongitudeText;
  
      @SuppressLint("SetTextI18n")
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          button3 = findViewById(R.id.button_3);
          button3.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  openMain2Activity();
              }
          });
  
          button4 = findViewById(R.id.button4);
          button4.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  openMain3Activity();
              }
          });
  
          button5 = findViewById(R.id.button5);
          button5.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  openMain4Activity();
              }
          });
  
          button6 = findViewById(R.id.button6);
          button6.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  openMain5Activity();
              }
          });
  
          mLatitudeLabel = getResources().getString(R.string.latitude_label);
          mLongitudeLabel = getResources().getString(R.string.longitude_label);
          mLatitudeText = findViewById((R.id.latitude_text));
          mLongitudeText = findViewById((R.id.longitude_text));
  
          mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
      }
  
      public void openMain2Activity(){
          Intent intent = new Intent(this, Main2Activity.class);
          startActivity(intent);
      }
  
      public void openMain3Activity(){
          Intent intent = new Intent(this, Main3Activity.class);
          startActivity(intent);
      }
      public void openMain4Activity(){
          Intent intent = new Intent(this, Main4Activity.class);
          startActivity(intent);
      }
      public void openMain5Activity(){
          Intent intent = new Intent(this, Main5Activity.class);
          startActivity(intent);
      }
  
      @Override
      public void onStart() {
          super.onStart();
  
          if (!checkPermissions()) {
              requestPermissions();
          } else {
              getLastLocation();
          }
      }
  
      @SuppressWarnings("MissingPermission")
      public void getLastLocation() {
          mFusedLocationClient.getLastLocation()
                  .addOnCompleteListener(this, new OnCompleteListener&lt;Location&gt;() {
                      @Override
                      public void onComplete(@NonNull Task&lt;Location&gt; task) {
                          if (task.isSuccessful() &amp;&amp; task.getResult() != null) {
                              mLastLocation = task.getResult();
  
                              mLatitudeText.setText(String.format(Locale.ENGLISH, "%s: %f",
                                      mLatitudeLabel,
                                      mLastLocation.getLatitude()));
  
                              latitude = mLastLocation.getLatitude();
                              String lat = Double.toString(latitude);
                              TextView textView = findViewById(R.id.textView);
                              textView.setText(lat);
  
                              mLongitudeText.setText(String.format(Locale.ENGLISH, "%s: %f",
                                      mLongitudeLabel,
                                      mLastLocation.getLongitude()));
  
                              longitube = mLastLocation.getLongitude();
                              String longt = Double.toString(longitube);
                              TextView textView2 = findViewById(R.id.textView2);
                              textView2.setText(longt);
  
                          } else {
                              Log.w(TAG1, "getLastLocation:exception", task.getException());
                              showSnackbar(getString(R.string.no_location_detected));
                          }
                      }
                  });
      }
  
      private void showSnackbar(final String text) {
          View container = findViewById(R.id.main_activity_container);
          if (container != null) {
              Snackbar.make(container, text, Snackbar.LENGTH_LONG).show();
          }
      }
  
      private void showSnackbar(final int mainTextStringId, final int actionStringId,
                                View.OnClickListener listener) {
          Snackbar.make(findViewById(android.R.id.content),
                  getString(mainTextStringId),
                  Snackbar.LENGTH_INDEFINITE)
                  .setAction(getString(actionStringId), listener).show();
      }
  
      private boolean checkPermissions() {
          int permissionState = ActivityCompat.checkSelfPermission(this,
                  Manifest.permission.ACCESS_COARSE_LOCATION);
          return permissionState == PackageManager.PERMISSION_GRANTED;
      }
  
      private void startLocationPermissionRequest() {
          ActivityCompat.requestPermissions(MainActivity.this,
                  new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                  REQUEST_PERMISSIONS_REQUEST_CODE);
      }
  
      private void requestPermissions() {
          boolean shouldProvideRationale =
                  ActivityCompat.shouldShowRequestPermissionRationale(this,
                          Manifest.permission.ACCESS_COARSE_LOCATION);
  
          if (shouldProvideRationale) {
              Log.i(TAG1, "Displaying permission rationale to provide additional context.");
  
              showSnackbar(R.string.permission_rationale, android.R.string.ok,
                      new View.OnClickListener() {
                          @Override
                          public void onClick(View view) {
                              // Request permission
                              startLocationPermissionRequest();
                          }
                      });
  
          } else {
              Log.i(TAG1, "Requesting permission");
              startLocationPermissionRequest();
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                             @NonNull int[] grantResults) {
          Log.i(TAG1, "onRequestPermissionResult");
          if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
              if (grantResults.length &lt;= 0) {
                  Log.i(TAG1, "User interaction was cancelled.");
              } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  
                  getLastLocation();
              } else {
                  showSnackbar(R.string.permission_denied_explanation, R.string.settings,
                          new View.OnClickListener() {
                              @Override
                              public void onClick(View view) {
                                  // Build intent that displays the App settings screen.
                                  Intent intent = new Intent();
                                  intent.setAction(
                                          Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                  Uri uri = Uri.fromParts("package",
                                          BuildConfig.APPLICATION_ID, null);
                                  intent.setData(uri);
                                  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                  startActivity(intent);
                              }
                          });
              }
          }
      }
  }
  </code></pre>
  
  <p>This is my <code>main2activty</code>.</p>
  
  <pre><code>package hk.android.1;
  
  import android.annotation.SuppressLint;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.widget.Toast;
  import android.os.Handler;
  import android.support.v4.widget.SwipeRefreshLayout;
  import android.util.Log;
  import com.android.volley.Request;
  import com.android.volley.RequestQueue;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.toolbox.StringRequest;
  import com.android.volley.toolbox.Volley;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  import java.util.ArrayList;
  import java.util.List;
  
  
  
  public class Main2Activity extends AppCompatActivity {
      private static final String TAG = "Main2Activity";
      SwipeRefreshLayout mySwipeRefreshLayout;
      private static final int PAGE_SIZE = 1000;
  
      @SuppressLint("SetTextI18n")
      public static void main(String[] args) {
          // ...
          clearScreen();
      }
  
      private static void clearScreen() {
          for (int i = 0; i &lt; PAGE_SIZE; i++) {
              System.out.println();
          }
      }
  
      private RecyclerView recyclerView;
      private RecyclerView.Adapter adapter;
  
      private List&lt;ListItem&gt; listItems;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main2);
          mySwipeRefreshLayout = findViewById(R.id.swiperefresh);
          mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
          {
              @Override
              public void onRefresh()
              {
                  Log.i(TAG, "onRefresh called from SwipeRefreshLayout");
                  myUpdateOperation();
              }
          });
  
          mySwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light,
                  android.R.color.holo_blue_light,
                  android.R.color.holo_green_light,
                  android.R.color.holo_orange_light,
                  android.R.color.holo_purple);
  
          recyclerView = findViewById(R.id.recyclerView);
          recyclerView.setHasFixedSize(true);
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
          listItems = new ArrayList&lt;&gt;();
  
          loadRecyclerViewData();
  
      }
  
      public void loadRecyclerViewData(){
      double latitude = MainActivity.latitude;
      double longitube = MainActivity.longitube;
      String language = getResources().getString(R.string.lang);
      //String language = Locale.getDefault().getLanguage();
      private String URL_DATA = "http://xxxx/toilet/json-toilet.php?lat=" + latitude + "&amp;lng=" + longitube + "&amp;lang=" + language;
          final ProgressDialog progressDialog = new ProgressDialog(this);
          StringRequest stringRequest = new StringRequest(Request.Method.GET,
                  URL_DATA,
                  new Response.Listener&lt;String&gt;() {
                      @Override
                      public void onResponse(String s) {
                          progressDialog.dismiss();
                          try {
                              JSONObject jsonObject = new JSONObject(s);
                              JSONArray array = jsonObject.getJSONArray("results");
  
                              for(int i=0; i&lt;array.length(); i++){
                                  JSONObject o = array.getJSONObject(i);
                                  ListItem item;
                                  item = new ListItem(
                                          o.getString("name"),
                                          o.getString("address"),
                                          o.getString("distance")
                                  );
                                  listItems.add(item);
                              }
                              adapter = new MyAdapter(listItems, getApplicationContext());
                              recyclerView.setAdapter(adapter);
                          }      catch (JSONException e) {
                              e.printStackTrace();
  
                          }
                      }
                  },
  
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError volleyError) {
                          progressDialog.dismiss();
                          Toast.makeText (getApplicationContext(), volleyError.getMessage(),Toast.LENGTH_LONG).show();
                      }
                  });
          RequestQueue requestQueue = Volley.newRequestQueue(this);
          requestQueue.add(stringRequest);
      }
  
  
      public void openMainActivity(){
          Intent intent = new Intent(this,MainActivity.class);
          startActivity(intent);
      }
  
      private void myUpdateOperation()
      {
          new Handler().postDelayed(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {
                                            mySwipeRefreshLayout.setRefreshing(false);
                                            openMainActivity();
                                        }
                                    }, 2555
          );
      }
  }
  </code></pre>
  
  <p>I expect that I can get the closest toilet after get the latitude and longitude information to <code>main2activity</code> and update the URL_DATA.</p>