Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android Process and activities life cycle

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2010, 08:10 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android Process and activities life cycle

Lets start simple:
Each Basic4android program runs in its own process.
A process has one main thread which is also named the UI thread which lives as long as the process lives. A process can also have more threads which are useful for background tasks.

A process starts when the user launches your application, assuming that it is not running already in the background.

The process end is less determinant. It will happen sometime after the user or system has closed all the activities.
If for example you have one activity and the user pressed on the back key, the activity gets closed. Later when the phone gets low on memory (and eventually it will happen) the process will quit.
If the user launches your program again and the process was not killed yet the same process will be reused.

A Basic4android application is made of one or more activities. Android support several other "main" components. These will be added to Basic4android in the future.

Activities are somewhat similar to Windows Forms.
One major difference is that, while an activity is not in the foreground it can be killed in order to preserve memory. Usually you will want to save the state of the activity before it gets lost. Either in a persistent storage or in memory that is associated with the process.
Later this activity will be recreated when needed.

Another delicate point happens when there is a major configuration change in the device. The most common is an orientation change (user rotates the device). When such a change occurs the current activities are destroyed and then recreated. Now when we create the activity we can create it according to the new configuration (for example, we now know the new screen dimensions).
How do we handle it?
When you create a new activity you will start with the following code template:
Code:
Sub Process_Globals
    
'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    
'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Variables can be either global or local. Local variables are variables that are declared inside a sub other than Process_Globals or Globals.
Local variables are local to the containing sub. Once the sub ends these variables no longer exist.
Global variables can be accessed from all subs.

There are two types of global variables.
Process variables and activity variables.

Process variables - These variables live as long as the process lives.
You should declare these variables inside sub Process_Globals.
This sub is called once when the process starts (this is true for all activities, not just the first activity).
These variables are the only "public" variables. Which means that they can be accessed from other modules as well.
However, not all types of objects can be declared as process variables.
All of the views for example cannot be declared as process variables.
The reason is that we do not want to hold a reference to objects that should be destroyed together with the activity.
In other words, once the activity is being destroyed, all of the views which are contained in the activity are being destroyed as well.
If we hold a reference to a view, the garbage collector would not be able to free the resource and we will have a memory leak.
The compiler enforces this requirement.

Activity variables - These variables are contained by the activity.
You should declare these variables inside Sub Globals.
These variables are "private" and can only be accessed from the current activity module.
All objects types can be declared as activity variables.
Every time the activity is created, Sub Globals is called (before Activity_Create).
These variables exist as long as the activity exists.

Sub Activity_Create (FirstTime As Boolean)
This sub is called when the activity is created.
The activity is created when the user first launches the application, the device configuration has changed (user rotated the device) and the activity was destroyed, or when the activity was in the background and the OS decided to destroy it in order to free memory.
This sub should be used to load or create the layout (among other uses).
The FirstTime parameter tells us if this is the first time that this activity is created. First time relates to the current process.
You can use FirstTime to run all kinds of initializations related to the process variables.
For example if you have a file with a list of values that you need to read, you can read it if FirstTime is True and store the list as a process variable.
Now we know that this list will be available as long as the process lives and there is no need to reload it even when the activity is recreated.

To summarize, you can test whether FirstTime is True and then initialize process variables.

Sub Activity_Resume and Sub Activity_Pause (UserClosed As Boolean)
Each time the activity moves from the foreground to the background Activity_Pause is called.
Activity_Pause is also called when the activity is in the foreground and a configuration change occurs (which leads to the activity getting paused and then destroyed).
Activity_Pause is the last place to save important information.
Generally there are two types of mechanisms that allow you to save the activity state.
Information that is only relevant to the current application instance can be stored in one or more process variables.
Other information should be stored in a persistent storage (file or database).
For example, if the user changed some settings you should save the changes to a persistent storage at this point. Otherwise the changes may be lost.

Activity_Resume is called right after Activity_Create finishes or after resuming a paused activity (activity moved to the background and now it returns to the foreground).
Note that when you open a different activity (by calling StartActivity), the current activity is first paused and then the other activity will be created if needed and (always) resumed.

As discussed above Activity_Pause is called every time that the activity moves from the foreground to the background. This can happen because:
1. A different activity was started.
2. The Home button was pressed
3. A configuration changed event was raised (orientation changed for example).
4. The Back button was pressed.

In scenarios 1 and 2, the activity will be paused and for now kept in memory as it is expected to be reused later.
In scenario 3 the activity will be paused, destroyed and then created (and resumed) again.
In scenario 4 the activity will be paused and destroyed. Pressing on the Back button is similar to closing the activity. In this case you do not need to save any instance specific information (the position of pacman in a PacMan game for example).
The UserClosed parameter will be true in this scenario and false in all other. Note that it will also be true when you call Activity.Finish. This method pauses and destroys the current activity, similar to the Back button.

You can use UserClosed parameter to decide which data to save and also whether to reset any related process variables to their initial state (move pacman position to the center if the position is a process variable).

A new module is available for handling the UI state: http://www.basic4ppc.com/forum/basic...ngs-state.html
Reply With Quote
  #2 (permalink)  
Old 11-15-2010, 04:36 PM
schimanski's Avatar
Basic4ppc Veteran
 
Join Date: Oct 2007
Location: Germany
Posts: 289
Default Is it similar to agraham's brilliant threading-lib?

I want to transform my prime application from windows mobile to android and one of the things I need for this is something like agrahams threading-lib. I have collected data from a server with an own thread and transvered the data over an global variable to the main programm. When the server-thread hangs up, it doesen't have any influence to the main program.
Have I understand it right, that basic4android is in the future able to manage two independent threads???

Thanks for your efforts....
__________________
schimanski
--------------------------------------
Device: Motorola Defy, Samsung Galaxy Tab P1000
Dekstop: Asus Eee PC
Reply With Quote
  #3 (permalink)  
Old 11-15-2010, 04:41 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Yes but note that the Http library is already has an asynchronous capability. Erel describes it here http://www.basic4ppc.com/forum/basic...ices-more.html
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #4 (permalink)  
Old 11-16-2010, 05:44 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I'll just add that Basic4android has several internal features dedicated for working with multiple threads. For now only the HTTP library uses these features, but I'm sure that in the future there will be several libraries that will use those.
Reply With Quote
  #5 (permalink)  
Old 11-16-2010, 10:00 AM
schimanski's Avatar
Basic4ppc Veteran
 
Join Date: Oct 2007
Location: Germany
Posts: 289
Default

Quote:
Originally Posted by Erel View Post
I'll just add that Basic4android has several internal features dedicated for working with multiple threads. For now only the HTTP library uses these features, but I'm sure that in the future there will be several libraries that will use those.

Thanks for this information. I think, that android in future is much suitable for my app...
__________________
schimanski
--------------------------------------
Device: Motorola Defy, Samsung Galaxy Tab P1000
Dekstop: Asus Eee PC
Reply With Quote
  #6 (permalink)  
Old 11-26-2010, 10:21 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by schimanski View Post
I need for this is something like agrahams threading-lib.
It's ready and tested waiting for the release of Basic4Android and the creation of the additional libraries forum.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #7 (permalink)  
Old 11-28-2010, 09:58 AM
schimanski's Avatar
Basic4ppc Veteran
 
Join Date: Oct 2007
Location: Germany
Posts: 289
Default

Thanks agraham!!!

That are very great news!!!
__________________
schimanski
--------------------------------------
Device: Motorola Defy, Samsung Galaxy Tab P1000
Dekstop: Asus Eee PC
Reply With Quote
  #8 (permalink)  
Old 12-26-2010, 08:17 PM
Newbie
 
Join Date: Dec 2007
Location: Greece
Posts: 5
Default

Merry Christmas and a Happy New Year!
I have a small question.
I made a simple application with several text boxes and two layout variants included.
When I change the orientation from one layout to another all the data of the text boxes are dissapear.
Which activity sub I must call to prevent this data loss?
Regards

Thanos

Last edited by thanos : 12-26-2010 at 08:21 PM.
Reply With Quote
  #9 (permalink)  
Old 12-27-2010, 06:40 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

You should save the required fields in process global variables during Activity_Pause and then load these values in Activity_Resume. See this example: http://www.basic4ppc.com/forum/basic...ices-more.html
Reply With Quote
  #10 (permalink)  
Old 01-04-2011, 05:53 PM
Newbie
 
Join Date: Dec 2010
Location: England
Posts: 2
Default Total Newbie - Layouts

Hi to all

I'm a complete Newbie as regards any type of programming.

I have managed to write a small program with a layout but would like to add another layout as a 'Disclaimer' type dialog that the user has to either Agree/Disagree before the main layout loads. I can work out how to load layouts but can't seem to find in the forums any ways to kill the 2nd layout to leave the main layout active. Or am I looking at this the wrong way?

Any advice would be helpful.

Apologies in advance for what may seem a silly question but we all have to learn from scratch sometime!

DunDroid
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
hold process Byak@ Questions (Windows Mobile) 6 07-10-2010 07:04 PM
File being used by another process jschuchert Questions (Windows Mobile) 21 12-05-2009 05:44 PM
Creating process-real!!! Byak@ Code Samples & Tips 6 10-06-2008 01:59 PM
Trignomic Cycle formula Cableguy Chit Chat 3 08-16-2008 04:13 PM
Process and Basic4ppc? Byak@ Questions (Windows Mobile) 3 07-22-2008 04:05 PM


All times are GMT. The time now is 10:12 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0