The names of callbacks on the life cycle have been inspired by Android's. There is an equivalent to each of them in iOS, their naming is more precise but more complicated, so they looked less nity.
As of now, only `App` specialize `AppComponent`, but more users in the UI module will soon be introduced.
The implementation of this life cycle in mnit and the old Android implementation using the NDK is a bit awkward. But you will notice that it is much better in the newer NitActivity based implementation, which is the future!
Pull-Request: #1343
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Ait younes Mehdi Adel <overpex@gmail.com>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
# Wanted screen height
var screen_height = 480
- redef fun window_created
+ redef fun on_create
do
super
game = new Game
# Zoom applied for the device display from the game logic coordinates
var zoom = 1.0
- redef fun window_created
+ redef fun on_create
do
super
init do super
- redef fun window_created
+ redef fun on_create
do
super
super
end
- redef fun window_created
+ redef fun on_create
do
super
screen = new Screen(self, display.as(Display))
var imgs : nullable ImageSet = null
var splash : nullable SplashScreen = null
- redef fun window_created
+ redef fun on_create
do
super
var screen: nullable Screen = null
- redef fun window_created
+ redef fun on_create
do
super
var img : nullable I = null
- redef fun window_created
+ redef fun on_create
do
super
var test_assets = false
var test_ressources = true
- redef fun window_created
+ redef fun on_create
do
super
if test_assets then
import app::data_store
redef class App
- redef fun window_created
+ redef fun on_create
do
super
var img_boss_left: Image
var img_boss_right: Image
- redef fun window_created
+ redef fun on_create
do
super
redef fun init_window
do
super
- window_created
+ on_create
+ on_restore_state
+ on_start
end
redef fun term_window
do
super
- window_closing
+ on_stop
end
# Is the application currently paused?
var paused = true
- redef fun window_created
- do
- super
- paused = false
- end
-
- redef fun window_closing
- do
- paused = true
- super
- end
-
redef fun pause
do
paused = true
+ on_pause
super
end
redef fun resume
do
paused = false
+ on_resume
super
end
+ redef fun save_state do on_save_state
+
redef fun lost_focus
do
paused = true
super
end
- redef fun destroy do exit 0
+ redef fun destroy do on_destroy
end
# impossible for you to compile the apk correctly
module assets_and_resources
-import native_app_glue
+import dalvik
import java
import java::io
import java
import java::io
import assets_and_resources
+import native_app_glue # FIXME update this module to use nit_activity
import app::audio
in "Java" `{
# as registered by `on_save_instance_state`.
#
# Followed by `on_start`.
- fun on_create(save_state: NativeBundle) do end
+ fun on_create(save_state: NativeBundle)
+ do
+ app.on_create
+ app.on_restore_state
+ end
# Notification from Android, the activity has been restarted
#
# Notification from Android, the activity has been started
#
# Followed by `on_resume` or `on_stop`.
- fun on_start do end
+ fun on_start do app.on_start
# Notification from Android, the activity has been resumed
#
# Followed by `on_pause`
- fun on_resume do end
+ fun on_resume do app.on_resume
# Notification from Android, the activity has been paused
#
# Followed by `on_resume` or `on_stop`.
- fun on_pause do end
+ fun on_pause do app.on_pause
# Notification from Android, the activity has been stopped
#
# Followed by `on_restart` or `on_destroy`.
- fun on_stop do end
+ fun on_stop do app.on_stop
# Notification from Android, the activity is being destroyed
#
# Clean up and exit.
fun on_destroy
do
+ app.on_destroy
+
native.delete_global_ref
app.activities.remove self
end
# Notification from Android, the activity may be stopped, save state
#
# Occurs before `on_stop` and, without guarantee, before or after `on_pause`.
- fun on_save_instance_state(save_state: NativeBundle) do end
+ fun on_save_instance_state(save_state: NativeBundle) do app.on_save_state
# Notification from Android, the system is running low on memory
#
# See the License for the specific language governing permissions and
# limitations under the License.
+# Base of the _app.nit_ framework, defines `App`
module app_base is
new_annotation app_name
new_annotation app_namespace
# App subclasses are cross-platform applications
#
-# This class is redefed by plateform modules and so
-# App can be specialized directly in the user app.
+# This class is refined by platform modules and so
+# App can be specialized directly in the user application code.
class App
+ super AppComponent
+
protected init do end
# Starts the internal setup of graphical and other stuff
# Main entry point of your application
fun run do end
+end
+
+# An element of an application that is notified of the application life cycle
+#
+# Most users of _app.nit_ need only to implement `on_create` to setup the application.
+#
+# On mobile devices, the application can be stopped a anytime when another application takes the foreground.
+# Implement the callbacks `on_save_state` and `on_load_state` to keep the state of the application between execution,
+# for an illusion of continuous execution.
+abstract class AppComponent
+
+ # The application is being created
+ #
+ # You should build the UI at this time.
+ fun on_create do end
- # Main init method for graphical stuff
- # Is called when display is ready so graphical assets
- # can be loaded at this time.
- fun window_created do end
+ # The application is starting or restarting, it is visible to the user
+ fun on_start do end
- # Called before destroying the window
- fun window_closing do end
+ # The application enters the active state, it is in the foreground and interactive
+ fun on_resume do end
+
+ # The application leaves the active state but is still partially visible
+ #
+ # It may still be visible in the background.
+ # It may then go back to `on_resume` or `on_stop`.
+ fun on_pause do end
+
+ # The application is completely hidden from the user
+ #
+ # It may then be destroyed (`on_destroy`) or go back to `on_start`.
+ fun on_stop do end
+
+ # The application is being destroyed
+ fun on_destroy do end
+
+ # The application may be destroyed soon, save its state for a future `on_restore_state`
+ fun on_save_state do end
+
+ # The application is launching, restore its state from a previous `on_save_state`
+ fun on_restore_state do end
end
# Print a warning
# The running `App`
fun app: App do return once new App
+
app.setup
app.run
do
super
display = new NullDisplay
- window_created
+ on_create
end
# Force the printing of blit operations
super
- window_created
+ on_create
+ on_restore_state
+ on_start
+ on_resume
+ end
+
+ redef fun run
+ do
+ super
+
+ on_pause
+ on_save_state
+ on_stop
+ on_destroy
end
redef fun generate_input