Codename One - "app" lifecycle

43 views
Skip to first unread message

P5music

unread,
Aug 14, 2020, 10:36:06 AM8/14/20
to CodenameOne Discussions
My Android app is simple but it handles rotation, start from an intent (cold and warm) (old intent or new), start from home, and so on. The methods that are called are not always know, so sometimes more than a single method are called. For example onSaveInstanceState and onConfigurationChanged are not always independent.
My app has to manage editing data in the detail view. For example it tries to ask the user whether it has to save, before going back or choosing other records.
In many case this has to be done silently because the app is just undergoing some strange eevent or transformation.
Furthermore the app singleton sometime is lost.

Codename apps seem to be simpler from this point of view, but I need to know if there is some "singleton" class I can count on every time, unless the app is destroyed.
I see that there is not fragment and activity cycles so I think this is easy.
Thanks in advance

Shai Almog

unread,
Aug 15, 2020, 1:27:55 AM8/15/20
to CodenameOne Discussions
Yes. Our activity works a bit as a singleton and simplified the horrendous Android app life cycle.
As a side note a few years ago in Google IO Romain Guy asked audience members for a show of hands if they know the Activity lifecycle. Hands were raised and he responded (I'm paraphrasing) "liars, I've been on the Android team since before it launched and I don't understand the Activity lifecycle".

I created a relatively simple app lifecycle diagram for the Uber clone book. The first two chapters (which include the diagram) are a free PDF download from here: https://uber.cn1.co/

P5music

unread,
Aug 16, 2020, 9:13:03 AM8/16/20
to CodenameOne Discussions
1-So the Codename app singleton is recreated just in what cases?
I think device orientation at least, and then?
2-Where can I put some global data structure, in the main form I guess?

Thanks in advance

Shai Almog

unread,
Aug 16, 2020, 9:41:59 PM8/16/20
to CodenameOne Discussions
Only in the case the app dies.
1. It's reused for device orientation.
2. You can put global data anywhere you want. I often just put settings in Preferences so they will remain between executions.

P5music

unread,
Aug 19, 2020, 5:13:48 AM8/19/20
to CodenameOne Discussions
I am passing references to the Containers and Forms that are called from the main app class, for example the parentForm or global data.
Said that the app is the singleton and only can die in certain cases (if I am not wrong), I woud like to know whether I have to use WeakReference so to avoid circular reference or I can just use normal references.
I hope this makes sense.
Thanks in advance

Shai Almog

unread,
Aug 19, 2020, 9:46:52 PM8/19/20
to CodenameOne Discussions
Use normal references. If the app dies it dies. We don't do the weird "partial death" behavior of Android.
You should try to respect stop() though and stop networking etc. in that case (with the special case of background processes).

P5music

unread,
Aug 20, 2020, 9:10:12 AM8/20/20
to CodenameOne Discussions
I have things like this in code:
FormSubClass formToCall=new FormSubClass(Form mainForm, AppData appData,...)

then showBack() is used when the back command is called in the new form.
Does this impact on the references?
I mean, a for is created every time. Is this bad practice?
Thanks

Shai Almog

unread,
Aug 21, 2020, 12:47:36 AM8/21/20
to CodenameOne Discussions
If you don't keep a reference to the form then it gets GC'd and it's a good practice to keep memory low. Recreating a form is relatively cheap.

P5music

unread,
Aug 21, 2020, 4:14:43 AM8/21/20
to CodenameOne Discussions
I have to keep the reference because I have to handle the back command. So I have to use another method or use WeakReference<>, as I understand.
Is this correct?

Shai Almog

unread,
Aug 22, 2020, 1:14:02 AM8/22/20
to CodenameOne Discussions
No. Keeping the reference to the parent form is fine (you don't recreate it). I meant don't keep the reference to the child form.
Weak references can get GC'd randomly. They're only meant for cache.
Reply all
Reply to author
Forward
0 new messages