Suspend service execution on OnResume (). Well, or write data to SharedPreferences and delete them from there when you exit the application. The class itself with preferences:
public class UserPreferences { private SharedPreferences sharedPreferences; private static UserPreferences userPreferences; public static UserPreferences getInstance(Context context) { if (userPreferences == null) { userPreferences = new UserPreferences(context); } return userPreferences; } private UserPreferences(Context context){ sharedPreferences = context.getSharedPreferences("sharedSettings",Context.MODE_PRIVATE); } public void storeUserInfo(String userCity){ SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("storeUserInfo", userCity); editor.commit(); } public String getUserInfo(){ return sharedPreferences.getString("storeUserInfo", null); } public void clearShared(){ SharedPreferences.Editor editor = sharedPreferences.edit(); editor.clear(); editor.commit(); }
}
Call this:
UserPreferences userPreferences = userPreferences.getInstance(getActivity()); userPreferences.getInstance(getActivity()).storeUserInfo("SomeInfo").
If you do not want to use the string in the preferences, then you can use the putInt () method. Further, it all depends on the wishes. In more detail about the life cycle of the application.