In my Application when a Button is clicked it redirects the user to Google Maps App on the mobile using the following code
Uri gmmIntentUri = Uri.parse("google.navigation:q=" + a + "," + b);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
now What I want is to when the user closes the Google Maps/ returns to My app, it should start a different activity rather than the one it was left on. Is it possible to do this? I've used a delay to start the secondary activity but it's not giving the result I need as sometimes it runs over the Google Maps app. Im fairly new to Android studio altogether.
Edit- is onSaveInstanceState is a possible way to overcome this issue?
There could be couple approaches to solve that issue. For example by using static Map<K,O>
and monitoring Activity
lifecycle. But because static
member should be handled properly to avoid leaking why not use SharedPreferences
.
For example in above part of code where you start Google Maps
you can save status in SharedPreferences
just call:
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean("maps", true)
.apply();
And when Google Maps
are launched your app will go in onPause
state so after user closes the Google Maps
your app will be resumed
so onResume
method will be called there you can check status:
@Override
protected void onResume() {
super.onResume();
boolean isComingFromMaps = PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("maps", false);
if(isComingFromMaps) {
//Launch your activity here
//also don't forget to save value back to false to avoid bug when next time Activity is started
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean("maps", false)
.apply();
}
}
Also last checking is to make sure if user doesn't return from maps rather he minimises both apps, or kill them you want to set value back to false
before super.onDestroy()
is called. Like:
@Override
protected void onDestroy() {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putBoolean("maps", false)
.apply();
super.onDestroy();
}