What you are trying to do, as far as I know, is not possible, since each activity has its own context (set of features and own classes), in addition, it is only possible to execute one Activity at a time, so it is not possible to capture the events of an Activity A that is "closed" within an Activity B that is open. So if you want some activity to capture the "changed location" event, then you should "listen" and handle changes to the location.
As in your case both activities need to receive changes in the location, you have two options:
When you get the new location in GPSTrackerActivity
, start the second activity by passing the location obtained through a Bundle
In this case, when you get the new location with the method onLocationChanged(Location location)
in GPSTrackerActivity
, you would do:
@Override
public void onLocationChanged(Location location) {
// Calcule a sua distancia
// double distancia = ?
Bundle mBundle = new Bundle();
mBundle.putDouble("distancia",distancia);
Intent mIntent = new Intent(this, SegundaActivity.class)
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
And in SegundaActivity
you would get the distance passed in method onCreate()
using the following code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
double distancia = getIntent().getExtras().getDouble("distancia");
/* ... */
}
Note:
Note that in this case, your SegundaActivity
only receives a single distance, which was obtained from the last location received by GPSTrackerActivity
. This means that if your location changes while SegundaActivity
is open, it will not receive this new distance, since the receiving and processing distance is GPSTrackerActivity
. If you would like SegundaActivity
to constantly receive a new location as there are changes, you can use the second approach.
Create a " MeuLocationManager
" class that will be responsible for tracking location changes and providing them to a "requester"
In this case, you will transfer all of the localization change tracking logic to the " MeuLocationManager
" class. So every time a class wants to receive information about location changes, you just have to ask " MeuLocationManager
" to send it to the locations it has retrieved. It looks something like this:
public class MeuLocationManager implements LocationListener /*, etc. */ {
// Classe que vai requisitar a localização
private LocationListener requisitorLocalizacao;
/* Vamos implementar o padrão Singleton para evitar que
um novo LocationManager seja criado a cada vez que for
necessário chamá-lo*/
private static MeuLocationManager instance;
private MeuLocationManager() {
/* Implemente e/ou crie todos os métodos necessários
para obtenção de localização, como:
Criação e inicialização do Google Services
Callbacks para escutar a conexão com o Google Services, etc. */
}
public static MeuLocationManager getInstance(){
if(instance == null){
return new MeuLocationManager();
}else{
return instance;
}
}
/* Método que será usado por outra classe para solicitar
a localização */
public void requestLocationUpdates(LocationListener requisitorLocalizacao){
this.requisitorLocalizacao = requisitorLocalizacao;
}
/* Repassa a localização obtida para o requisitor da
localização */
@Override
public void onLocationChanged(Location location){
// Repassa a localização obtida para o requisitor da
// localizacao
requisitorLocalizacao.onLocationChanged(location);
}
}
Now, any class that wants to receive changes to the location should implement the LocationListener
interface and ask MeuLocationManager
to send it the new locations it has obtained. Let's use as an example its class SegundaActivity
. First, you should have it implement the LocationListener
interface, then you will do the following in your onCreate()
method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MeuLocationManager locationManager = MeuLocationManager.getInstance();
// Solicita localização
locationManager.requestLocationUpdates(this);
}
// No método 'onLocationChanged' faça o que você quiser
// com a localização que foi recebida do MeuLocationManager
@Override
public void onLocationChanged(Location location) {
/* ... */
}
These are only two possible solutions, there are many others, such as using the Reactive Location library, which is the one I use in my projects:)