Android tutorial (2.1) activity details

Source: Internet
Author: User

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/gooogledev/archive/2009/02/20/3914924.aspx

 

[Body]

Activity is the most important class for developing Android applications. This class has a lot of content. How much do I understand?
I hope you like it :)

This article involves the following content:

Lifecycle of an activity
2. Change activity to a window: Activity attribute setting
3. What should I do if your background activity is recycled by the system: onsaveinstancestate?
4. call and call: our communication messenger-Intent

Lifecycle of an activity

Like applications on other mobile platforms, the lifecycle of Android applications is under unified control.
That is to say, the fate of the application we write is in the hands of others (systems). We can only learn and
Adapt to it.

To put it simply, why is this: when we run an application on our mobile phone, we may call in.
The program will be interrupted when the SMS is sent or there is no power, and the basic functions of the service phone are prioritized.
The external system does not allow you to occupy too many resources. At least ensure the phone function, so when resources are insufficient
Can be killed.

To put it bluntly, the following code shows the basic lifecycle of an activity:

 

Java code
Public class myactivity extends activity {
Protected void oncreate (bundle savedinstancestate );

Protected void onstart ();

Protected void onresume ();

Protected void onpause ();

Protected void onstop ();

Protected void ondestroy ();
}
Public class myactivity extends activity {
Protected void oncreate (bundle savedinstancestate );

Protected void onstart ();

Protected void onresume ();

Protected void onpause ();

Protected void onstop ();

Protected void ondestroy ();
}

 

The activities you write will reload these methods as needed. oncreate is inevitable. During the normal startup of an activity, they are called in the order of oncreate-> onstart-> onresume, when the activity is killed, the sequence is onpause-> onstop-> ondestroy. This is a complete life cycle, but someone asks, the program is running and the phone is called, what should I do with this program? If a new activity is in full screen when it is aborted: onpause-> onstop, onstart-> onresume when it is restored, if the application is interrupted by an activity whose theme is translucent or dicent, it is only onpause and onresume is restored.

The following describes in detail what the system is doing and what we should do:

Oncreate: Create an interface to initialize data.

Onstart: In this step, the user can see and cannot interact with each other.

Onresume: it becomes interactive with the user. (in the activity stack system, you can manage these
At the top of the activity, after running the pop-up stack, return to the previous activity)

Onpause: This step is visible but interactive, and the system will stop animation and other CPU-consuming tasks.
We know from the description above that some of your data should be saved here, because at this time
Your program has a lower priority and may be withdrawn by the system. The data stored here should be stored in

Onresume is read out. Note: This method takes a short time because the next
Activities will not be started until this method is completed

Onstop: becomes invisible and overwritten by the next activity

Ondestroy: This is the last method called before the activity is killed. It may be that the external class calls the finish party.
Method or the system can use isfinishing () to temporarily kill it to save space.
Break it. If you have a progress dialog online rotation, please go to ondestroy
Drop the cancel. Otherwise, when the thread ends, the cancel method called by dialog will be discarded.
Unusual.

Onpause, onstop, and ondestroy, all activities may be killed by the system in three states.
To ensure the correctness of the program, you need to write the code for the persistent layer operation in onpause () and save all the edited content to the storage medium (generally databases ). In actual work, there are also many problems due to changes in the life cycle. For example, if your application starts to run a new thread and the thread is interrupted, you need to maintain the thread, pause, kill, or roll back data, right? Because the activity may be killed, pay attention to the variables and some interface elements used in the thread. Generally, I use the android message mechanism [handler, message] to handle the problem of multithreading and interface interaction. I will talk about this later, because these things are already very big recently, and I will share them with you when I clear my thoughts.

2. Change activity to a window: Activity attribute setting

It's easy to talk about it. Some people may want to make the app something floating on the main interface of the mobile phone.
You only need to set the activity topic to define the activity in androidmanifest. xml.
Local sentence:

XML Code
Android: theme = "@ Android: style/theme. Dialog"
Android: theme = "@ Android: style/theme. Dialog"
This makes your application pop up in the form of a dialog box, or

XML Code
Android: theme = "@ Android: style/theme. translucent"
Android: theme = "@ Android: style/theme. translucent"
It becomes translucent. [friendly reminder -. -] attributes of similar activities can be found in Android. r. the androidmanifestactivity method of the styleable class shows androidmanifest. for more information about the attributes of all elements in XML, see android. r. styleable

The above is the property name. The specific value is in Android. r. style, such as @ Android: style/theme. dialog "corresponds to Android. r. style. theme_dialog, (replace '_' '. '<-- Note: The content of this article is not a smiley face. You can use it in the description file to find the ing between the class definition and the description file.

3. What should I do if your background activity is recycled by the system: onsaveinstancestate?

When one activity a in your program is running, it actively or passively runs another activity B
At this time, a will execute

 

Java code
Public void onsaveinstancestate (bundle outstate ){
Super. onsaveinstancestate (outstate );
Outstate. putlong ("ID", 1234567890 );
}
Public void onsaveinstancestate (bundle outstate ){
Super. onsaveinstancestate (outstate );
Outstate. putlong ("ID", 1234567890 );
}
B will come to a again after completion. There are two situations at this time: A is recycled, and A is not recycled.
The oncreate () method must be re-called for the received a. It is different from directly starting the oncreate () method with parameters.
Savedinstancestate. If it is not recovered, it will be onresume.

Savedinstancestate is a bundle object. You can basically regard it as a map object maintained by the system. You may use it in oncreate (). If oncreate () is started normally, it will not be available. Therefore, you need to determine whether it is empty when using it.

 

Java code
If (savedinstancestate! = NULL ){
Long id = savedinstancestate. getlong ("ID ");
}
If (savedinstancestate! = NULL ){
Long id = savedinstancestate. getlong ("ID ");
}
Just like in the official notepad tutorial, if you are editing a note that is suddenly interrupted, remember the Note ID, then, you can extract the note according to the ID, and the program will be complete. This also shows that your application does not need to be saved. For example, if your interface is to read a list, you do not need to remember anything special. Oh, maybe you need to remember the position of the scroll bar...

4. call and call: our communication messenger intent

Intent is the intention. Intent communicates with each other, makes a call, and makes a call.
Intent will be sent over the phone. This is the essence of loose coupling of the android architecture, which greatly improves the reusability of components. For example, you need to click a button in your application, it's easy to call someone. Let's look at the code first:

 

Java code
Intent intent = new intent ();
Intent. setaction (intent. action_call );
Intent. setdata (URI. parse ("Tel:" + number ));
Startactivity (intent );
Intent intent = new intent ();
Intent. setaction (intent. action_call );
Intent. setdata (URI. parse ("Tel:" + number ));
Startactivity (intent );

Throwing out such an intention, the system will wake up the dialing program and make a call when it sees your intention. What reading contacts, text messages, emails, all just need to be thrown out of intent. This part is really well designed.

So what does intent tell the system who needs to accept it?
There are two methods to use intent. The first method is to directly describe which class is required to receive the Code as follows:

 

Java code
Intent intent = new intent (this, myactivity. Class );
Intent. getextras (). putstring ("ID", "1 ");
Tartactivity (intent );
Intent intent = new intent (this, myactivity. Class );
Intent. getextras (). putstring ("ID", "1 ");
Tartactivity (intent );

The first method is obvious. You can directly specify myactivity as the receiver and pass some data to myactivity. In myactivity, you can use getintent () to get the intent and data.

In the second method, you need to first check the intentfilter configuration in androidmenifest.

 

XML Code
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>
<Action Android: value = "android. Intent. Action. Edit"/>
<Action Android: value = "android. Intent. Action. Pick"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "Vnd. Android. cursor. DIR/vnd. Google. Note"/>
</Intent-filter>
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>
<Action Android: value = "android. Intent. Action. Edit"/>
<Action Android: value = "android. Intent. Action. Pick"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "Vnd. Android. cursor. DIR/vnd. Google. Note"/>
</Intent-filter>

 

If action, data, and category are used in the configuration, you must think that intent also has these things. Will the receiver be found after a match?

Action is actually a string name of intent.
The configuration file of intent-filter in the above section shows that this activity can accept different actions. Of course, the corresponding program logic is different. Let's mention the mimetype, which is defined in contentprovider, if you implement a contentprovider by yourself, you must specify mimetype to enable data to be used by others.

I don't know the principle. I don't want to explain it. In summary, you call another interface instead of the new interface. Instead, you can throw an intent to let the system call that interface for you, in this way, it is loose and compliant with the principle that the lifecycle is managed by the system.

If you want to know what category has and what actions android has customized for you, visit the official link intent.

PS: If you want to know how to call system applications, you can take a closer look at your logcat. Do you have some information when running a program? For example:
Starting activity: intent {Action = android. intent. action. main categories = {android. intent. category. launcher} flags = 0x10200000 comp = {COM. android. camera/COM. android. camera. gallerypicker }}
Compare some set methods of intent to know how to call them. Hope you like it :)

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.