2

I have a service listening to some events from server. A service has START_STICKY flag that makes him restart when it's killed by OS. When service receive an event i have two scenarios. First, if activity isn't killed i need to send result to local broadcast receiver and update UI. Second, if it's killed by OS i want to recreate it and send data in bundle.

But i don't know how to recognize that android killed my activity. onDestroy activity event doesn't come in this situation.

    @Override
    public void onComplete(CurrentOrdersResponse response) {
        if (response == null) {
            return;
        }
        boolean isActivityDestroyed = mPreferences.getBoolean(MainActivity.IS_MAIN_ACTIVITY_DESTROYED_PREF_KEY, false);
        if (!isActivityDestroyed)
            sendResult(response.getResJSONStr(), CURRENT_ORDERS_ACTION);
        else {
            Intent intent = new Intent(this, MainActivity.class);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtras(extras);
            startActivity(intent);
        }
        int resCode = response.getResCode();
        Log.i(LOG_TAG, "Service resCode" + " " + resCode);
    }
  • is it not enough if you know whether it is visible or not ? If so, http://stackoverflow.com/a/5446680/3209739 is not enough ? – cgr Dec 15 '15 at 16:55
  • No. It's not enough. I need to update my UI even if it's in background(stopped and paused) – Soslan Kusraev Dec 15 '15 at 17:38
  • 1
    Then you should also check if it's available in back stack of its not visible. If it's in stack them update there. – cgr Dec 15 '15 at 17:51
  • I have seen that solution. But someone has said it should be used only for debug purposes. http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks%28int%29 – Soslan Kusraev Dec 15 '15 at 17:54
  • I am not sure where exactly it is told that but back stack is the place we look for activities/fragments if they are available already and do the things accordingly. You may think of posting another question asking if there is an alternative to find if activity/fragment object is visible but not visible. Through PackageManager you can know if the class itself exists or not in the device. – cgr Dec 15 '15 at 19:32
  • i attached a link to developers.android.com for previous comment – Soslan Kusraev Dec 15 '15 at 21:54
  • That just takes me to ActivityManager whole doc. I could not find where that is mentioned. – cgr Dec 15 '15 at 21:58
  • Just scroll to getRunningTasks method. Not to mention the fact that activity stays in task info even if it's already killed. Just have tried it. Thank you for your responsing. This is very important for me. – Soslan Kusraev Dec 16 '15 at 00:25

1 Answers1

5

It sounds like you are using LocalBroadcastManager. That's good. Its sendBroadcast() method returns a boolean indicating if a registered receiver was found. You can use that result to determine if your receiving activity (MainActivity) exists and has registered to receive the broadcast.

When your service has an event to send to MainActivity, first try to send the event using sendBroadcast(). If it returns true, your done. If it returns false, the activity is not registered and must be created using startActivity(), with the event passed as an extra, as shown in your posted code.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • OMG! Thank you. You're right! Even in android javadocs there's nothing about returning value of sendBroadcast(). I didn't expect such an easy solution! – Soslan Kusraev Dec 16 '15 at 13:02
  • 1
    There is an open [AOSP Issue](https://code.google.com/p/android/issues/detail?id=59626) for the problem with the documentation. I looked at the source code to learn what the return value of sendBroadcast() means. – Bob Snyder Dec 16 '15 at 14:42