Basic4android development tutorial (5) android process and activitys Lifecycle

Source: Internet
Author: User

In short:
Each basic4android program runs in its own process.
A process has a main thread, which is also called a UI thread. It runs until the process ends. A process can have several threads which are used for background work.

When a user starts an application, a process starts to work, assuming it has not been running in the background.

The process ends uncertain. Sometimes it may happen after the user or the system closes all activities.
Suppose you have an activity. When the user presses the return key, the activity is disabled. in the future, when the phone memory is insufficient, this process will exit (this will certainly happen in the end ).
If the process is not killed when the user runs the program again, the process will be used again.

A basic4android application is composed of one or more activities. Android supports several other "Main" components. These components will be added to later versions of basic4android.

Activities are similar to wondows forms.
The main difference is that an activity may be killed to save memory when it is not in the foreground. before the activity disappears, you need to save its status. either saved to a persistent storage or saved to the memory of its processes.
So that you can re-create this activity later.

Another thing to be careful about is when the main configuration of the device changes. the most common situation is the change in the direction (the user rotates the device ). when these changes occur, the current activities will be destroyed and rebuilt. now when we create an activity, we can create it based on the new configuration (for example, we now know the new screen size ).
How can we deal with this?
When creating a new activity, you can use 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 global and local. Local variables are defined in the Child Program rather than process_globals and globals.
Local variables are restricted in the subprocess. Once the child process ends, these variables no longer exist.
Global variables can be accessed by all child programs.

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

Process variable-These variables are consistent with the lifetime of the process.
You should define these variables in sub process_globals.
At the beginning of the process, the child program is called once (not only the first activitie, but also all activities ).
These variables should only be "public" variables, which means they can also be accessed by other modules.
However, not all types of objects can be defined as process variables.
For example, all views cannot be defined as process variables.
The main reason is that we do not want to keep a reference object that may be destroyed with activity destruction.
In other words, once the activity is destroyed, all views on the activity are also destroyed.
If a view reference is retained, the garbage collector cannot release these resources, causing memory leakage.
The compiler enforces this requirement.

Activity variable-These variables are included in the activity.
You should define these variables in sub globals.
These variables are "private" and can only be accessed in the current activity module.
All object types can be defined as activity variables.
Each time an activity is created, sub globals will be called (before activity_create ).
The lifetime of these variables is the same as that of the activity.

Sub activity_create (firsttime as Boolean)
After the activity is created, the child program is called.
When a user starts an application, the activity is created. When the configuration of the device changes (the user rotates the device), the activity is destroyed. in addition, when the activity is in the background, the OS may decide to destroy it to release the memory.
This child program should be used to load or create layout (of course it can be used for other purposes ).
The firsttime parameter is used to tell us if this activity is created for the first time. The firsttime parameter is relative to the current process.
You can use firsttime to initialize process variables.
For example, if you want to read a file containing a list, when firsttime is true, you can read this file and save the list in the file as a process variable.
Now we can know that the list will exist throughout the process, instead of re-reading the list every time the activity is created.

All in all, you can check whether firsttime is true and then initialize the process variable.

Sub activity_resume and sub activity_pause (userclosed as Boolean)
Every time the activity is converted from the foreground to the background, the activity_pause sub-process is called.
When the activity is in the foreground but the configuration changes (this will cause the activity to be paused and destroyed), The activity_pause sub-process will also be called.
Activity_pause is the last place to save important information.
There are two mechanisms for you to save the activity status.
Only information related to the current application can be saved to one or more process variables.
Other information should be saved to persistent storage (files or databases ).
For example, if you change some settings, you should save these changes to persistent storage. Otherwise, these changes may be lost.

After activity_create is complete, activity_resume will be called immediately. In addition, when a temporary activity is resumed, it will be called (the activity that is switched to the background will be switched to the foreground ).
Note: When you open a different activity (by calling startactivity), the current process is paused first, and another activity is created (if needed ), then the current process will continue to execute (always like this ).

Through the above discussion, activity_pause is called every time the activity is switched from the foreground to the background. This may be due to the following reasons:
1. Started a different activity.
2. The Home Key is pressed.
3. The configuration change event is triggered (for example, the direction changes ).
4. The backj key is pressed.

In the case of 1 and 2, the activity will be paused and saved to the memory for future use.
In the case of 3, the activity will be paused, destroyed, and then created (and continued ).
In case of 4, the activity will be paused and then destroyed.Press the back key, similar to closing the activity.In this case, you do not need to save any instance-specific information (such as the pacman location in the Pacman game ).
In this case, the userclosed parameter is true. it is false in other cases. note that when you call activity. it is also true when you finish. this method pauses and destroys the current activity, similar to the back key.

You can use the userclosed parameter to determine whether to save the data and reset any related process variables to the initialization value (if the position is a process variable, move the position of pacma n to the center ).

A new module that processes the UI status can be found here: http://www.basic4ppc.com/forum/basic...ngs-state.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.