Android activity-related

Source: Internet
Author: User
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


And Applications on other mobile platformsProgramSimilarly, the lifecycle of Android applications isUnified Control
, Also
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 basic lifecycle of an activity is as follows:CodeAs shown in:


Java code
  1. Public ClassMyactivityExtendsActivity {
  2. Protected VoidOncreate (bundle savedinstancestate );
  3. Protected VoidOnstart ();
  4. Protected VoidOnresume ();
  5. Protected VoidOnpause ();
  6. Protected VoidOnstop ();
  7. Protected VoidOndestroy ();
  8. }
 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 activity you write willAs needed
To overload these methods, oncreate is inevitable. In the normal startup process of an activity, the order in which they are called isOncreate-> onstart-> onresume,
When the activity is killed, the order isOnpause-> onstop-> ondestroy
This is a complete life cycle, but someone asks, the program is running and there is a call. What should this program do? If a new activity is displayed in full screen when the activity is aborted:Onpause-> onstop
, Recovery timeOnstart-> onresume
If the theme that interrupts this application isTranslucent
OrDialog
The activity isOnpause
, Recovery timeOnresume
.

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

Oncreate:

Create an interface here to initialize some data

Onstart:

In this stepInvisible to users
Of

Onresume:

Convert to andUser Interaction
(The activity stack system manages these items through stacks.
At the top of the activity, after running the pop-up stack, return to the previous activity)

Onpause:

In this stepVisible but not interactive
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

Read from onresume,Note: This method takes a short time because the next

Activities will not be started until this method is completed

Onstop:

BecomeInvisible
Is overwritten by the next activity.

Ondestroy:

This is the last method called before the activity is killed. It may be the finish method called by the external class.
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.

Differences

Regular
.



Onpause, onstop, ondestroy, all three States of activity may be SystemKill

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, because of the lifecycle
There are also many problems caused by changes in the phase. For example, if your application is running with a new thread, the thread is interrupted and you need to maintain the thread, pause, kill, or roll back data, right? Because
Activity may be killed, so you must 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
    1. 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
    1. Android: Theme="@ Android: style/theme. translucent"
 
Android: theme = "@ Android: style/theme. translucent"

 

It becomes translucent. the attribute of a similar activity can be [friendly prompt -.-].In Android. R. styleable
ClassAndroidmanifestactivity
The method shows that the attributes of all elements in androidmanifest. XML can be described by referring to Android. R. styleable.

The above is the property name. What is the specific value?In Android. R. Style
We can see that, for example"@ Android: style/theme. Dialog"
CorrespondsAndroid. R. style. theme_dialog
,('_' '.'
<-- Note: This isArticleContent is not a smiley face) can be used in the description file, find the class definition and the corresponding relationship in the description file to understand.

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
    1. Public VoidOnsaveinstancestate (bundle outstate ){
    2. Super. Onsaveinstancestate (outstate );
    3. Outstate. putlong ("ID",1234567890);
    4. }
 
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
    1. If(Savedinstancestate! =Null){
    2. LongId = savedinstancestate. getlong ("ID");
    3. }
 
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 depends on what your application does not need to save,For example, if your interface is to read a list, you don't 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: This is the intent.Intention
, Intent between applications, make 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
    1. Intent intent =NewIntent ();
    2. Intent. setaction (intent. action_call );
    3. Intent. setdata (URI. parse ("Tel :"+ Number ));
    4. 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
    1. Intent intent =NewIntent (This, Myactivity.Class);
    2. Intent. getextras (). putstring ("ID","1");
    3. 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
  1. <Intent-Filter>
  2. <Action Android: Name="Android. Intent. Action. View" />
  3. <Action Android: Value="Android. Intent. Action. Edit" />
  4. <Action Android: Value="Android. Intent. Action. Pick" />
  5. <Category Android: Name="Android. Intent. Category. Default" />
  6. <Data Android: mimetype="Vnd. Android. cursor. DIR/vnd. Google. Note" />
  7. </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 above shows that this activity can accept different actions. Of course, the corresponding program logic is different.
Mimetype defined in contentprovider. If you implement a contentprovider, you must specify
Mimetype can 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 :)

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.