Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 451a5c1

Browse files
[[ LifecycleListeners ]] Add ability to register for lifecycle events on Android
This patch adds methods to the Android `Engine` class to enable subscription to the `onPause` and `onResume` lifecycle events. Subscription is done by providing an object implementing the `LifecycleListener` interface. The ability is designed to be used internally, and to be wrapped by LCB utility methods.
1 parent cd1a536 commit 451a5c1

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

engine/src/java/com/runrev/android/Engine.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979

8080
public class Engine extends View implements EngineApi
8181
{
82+
public interface LifecycleListener
83+
{
84+
public abstract void OnResume();
85+
public abstract void OnPause();
86+
}
87+
8288
public static final String TAG = "revandroid.Engine";
8389

8490
// This is true if the engine is not suspended.
@@ -143,6 +149,8 @@ public class Engine extends View implements EngineApi
143149

144150
private int m_night_mode;
145151

152+
private List<LifecycleListener> m_lifecycle_listeners;
153+
146154
////////////////////////////////////////////////////////////////////////////////
147155

148156
public Engine(Context p_context)
@@ -248,6 +256,8 @@ public void onScreenOrientationChanged(int orientation)
248256
m_night_mode =
249257
p_context.getResources().getConfiguration().uiMode &
250258
Configuration.UI_MODE_NIGHT_MASK;
259+
260+
m_lifecycle_listeners = new ArrayList<LifecycleListener>();
251261
}
252262

253263
////////////////////////////////////////////////////////////////////////////////
@@ -3368,6 +3378,14 @@ public void onAppLaunched()
33683378

33693379
public void onPause()
33703380
{
3381+
/* Pause registered listeners in reverse order of registration as
3382+
* then components will be paused before any components they
3383+
* depend on. */
3384+
for (int i = m_lifecycle_listeners.size() - 1; i >= 0; i--)
3385+
{
3386+
m_lifecycle_listeners.get(i).OnPause();
3387+
}
3388+
33713389
if (m_text_editor_visible)
33723390
hideKeyboard();
33733391

@@ -3440,7 +3458,15 @@ public void onResume()
34403458

34413459
// IM-2013-08-16: [[ Bugfix 11103 ]] dispatch any remote notifications received while paused
34423460
dispatchNotifications();
3443-
3461+
3462+
/* Resume registered listeners in order of registration as then
3463+
* components will be resumed before any components which depend
3464+
* on them. */
3465+
for (int i = 0; i < m_lifecycle_listeners.size(); i++)
3466+
{
3467+
m_lifecycle_listeners.get(i).OnResume();
3468+
}
3469+
34443470
if (m_wake_on_event)
34453471
doProcess(false);
34463472
}
@@ -3994,7 +4020,31 @@ public int getSystemAppearance()
39944020
return 0;
39954021
}
39964022
}
3997-
4023+
4024+
////////////////////////////////////////////////////////////////////////////////
4025+
4026+
public boolean registerLifecycleListener(LifecycleListener p_listener)
4027+
{
4028+
return m_lifecycle_listeners.add(p_listener);
4029+
}
4030+
4031+
public boolean unregisterLifecycleListener(LifecycleListener p_listener)
4032+
{
4033+
/* We can't remove the listener directly since LifecycleListener does
4034+
* not implement equals. Instead, search backwards through the array
4035+
* until we find the passed listener. */
4036+
for (int i = m_lifecycle_listeners.size() - 1; i >= 0; i--)
4037+
{
4038+
if (m_lifecycle_listeners.get(i) == p_listener)
4039+
{
4040+
m_lifecycle_listeners.remove(i);
4041+
return true;
4042+
}
4043+
}
4044+
4045+
return false;
4046+
}
4047+
39984048
////////////////////////////////////////////////////////////////////////////////
39994049

40004050
// url launch callback

0 commit comments

Comments
 (0)