code_example icon indicating copy to clipboard operation
code_example copied to clipboard

Info request on BackgroundManager.java - WebSocket example

Open maxbaldrighi opened this issue 7 years ago • 0 comments

@alexz89ua, I came across your post when I had just decided to use in my project Kawasaki's nv-websocket-client.

It's the first time I have to implement a WebSocket instead of a plain HTTP connection and your post is really helping me out. I'm trying to build a simple sample app connecting to a WebSocket and sending random strings which are then caught by the socket and displayed in a RecyclerView.

There's just one thing I cannot understand, but it's not direclty WebSocket-related.

I know that the ActivityLifecycleCallbacks are triggered by any activity passing from a step of its cycle to another. The problem I see is in the implementation of onActivityPaused(): if (!mInBackground && mBackgroundTransition == null) { mBackgroundTransition = new Runnable() { @Override public void run() { mInBackground = true; mBackgroundTransition = null; notifyOnBecameBackground(); Log.i(LOG, "Application went to background"); } }; mBackgroundDelayHandler.postDelayed(mBackgroundTransition, BACKGROUND_DELAY); }

I tried to add another activity, started by a button click in my MainActivity, I registered it to BackgroundManager in onResume(), and unregistered it in onPause(). When I simply finish this second activity to return to the main one, here's what happens debugging:

  1. onActivityPaused is called. The condition is satisfied, the instance of mBackgroundTransition is created, BUT the code never enters into the run() method in the Runnable.
  2. onPause is called inside the finishing activity where I unregister the listener: @Override protected void onPause() { super.onPause(); BackForegroundManager.getInstance().unregisterListener(this); }

Everything on the contrary goes smoothly in 1. when the main activity (and with it, Application) goes actually in background. So the main goal is reached.

So here's my question in the end: Why isn't the debugger entering the run() method even if the condition is met and mBackgroundTransition is created correctly, unless the Application (and not a mere Activity) goes actually in background?

Sorry if the question is too long, but I wanted to be as clear as possible, and I hope it is.

maxbaldrighi avatar Sep 02 '17 03:09 maxbaldrighi