- Developers
- Professional Navigation for Fleets
- Android
- FAQ
- Problem of fragment being destroyed by OS
Problem of fragment being destroyed by OS
Sometimes Android OS destroys fragments in case of low memory while the app is in the background. This may apply to Sygic navigation as it is placed in a fragment.
This issue is related to the Embedded Integration only.
To cope with the problem the new SygicFragment methods have been introduced in order to remain navigation running even when SygicFragment is destroyed.
This is not enabled by default. So in order to activate this mechanism please check the following functions:
-
setAutoShutdownNavigation(boolean autoShutdown)
to set flag indicating whether the navigation should be shutdown when SygicFragment has been destroyed or should remain running -
setServiceNotification(int notificationId, Notification notification)
to set Sygic services run in foreground and make them less likely to be killed by system. Also, you set custom foreground service notification for them -
shutdownNavigation()
needs to be called when you want to shutdown navigation manually. Should be used only when setAutoShutdownNavigation(false) has previously been called
Example
This example show the modification of Sygic Fragment properties within the onResume() method to be less likely to be killed by Android OS.
public class SygicNaviFragment extends SygicFragmentSupportV4 {
@Override
public void onResume() {
setCallback(new ApiCallback());
// Call this, if you don't want navigation to be shut down when fragment is destroyed.
// Note that it is then your responsibility to shut down navigation with SygicFragment.shutdownNavigation().
// If the navigation keeps running in background, behaviour may be unpredictable.
setAutoShutdownNavigation(false);
final Intent intent = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName());
final PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.app_icon)
.setContentTitle("Sample title")
.setContentText("Sample text")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
// This call will make Sygic services run in foreground and therefore make them less likely to be killed by system.
setServiceNotification(2, notification);
startNavi();
super.onResume();
}
At the same time the method shutdownNavigation() needs to be applied in the Main Activity onDestroy() method.
@Override
protected void onDestroy() {
// Use this only when using SygicFragment.setAutoShutdownNavigation(false).
sygicNaviFragment.shutdownNavigation();
super.onDestroy();
}
- Previous article: How to use itineraries
- Next article: SygicFragment crash with UnsatisfiedLinkError