Enterprise-level Android Reading Notes (2)

Source: Internet
Author: User

Four components of Android

Android administrator: Activity

The activity manages and schedules the operations you want to perform on the screen.

All user operation classes must inherit from the activity

For example

@ Override

Public Boolean onkeydown (INT keycode, keyevent event ){

// Process the pressing event

}

If an activity is paused or stopped, the system can remove it from the memory by asking it to end (by calling its finish () method) or simply killing its process. When it is displayed to the user again, it must be completely restarted and restored to the previous state. As the activity changes from one status to another, it is notified by calling the following protected method:

  • Void oncreate (bundleSaveinstancestate)
  • Void onstart ()
  • Void onrestart ()
  • Void onresume ()
  • Void onpause ()
  • Void onstop ()
  • Void ondestroy ()

All these methods are hooks. You can rewrite them to do the appropriate work when the status changes. All activities must implement oncreate () to perform some initialization settings, when the object is first instantiated. Many activities also implement onpause () to submit data changes or prepare to stop interaction with users.

Merge them together. These seven methods define the entire lifecycle of an activity. There are three nested loops that you can monitor using these seven methods:

  • The entire lifecycle of the activity.From the first call of oncreate () until the call of ondestroy () ends. In oncreate (), an activity performs initial settings for all the "Global" states and releases all the reserved resources in ondestroy. For example, if a thread runs on the background to download data from the network, it may create a thread in oncreate () and end the thread in ondestroy.
  • Visible life time of an activityFrom onstart () to onstop (). During this period, the user can see the activity on the screen, although it may not run on the foreground and interact with the user. Between the two methods, you can maintain the resources required to display the activity. For example, you can register a broadcast receiver in onstart () to monitor changes that affect your UI and log out in onstop. Because the activity switches back and forth between visualization and hiding, onstart () and onstop () can be called multiple times.
  • Active foreground life timeFrom onresume () to onpause (). During this period, the switch between reuse and pause statuses is frequently-for example, when a device goes to sleep or a new activity starts, onpause () is called (), onresume () is called when an activity is returned or a new intent is transmitted (). Therefore, the code for these two methods should be quite lightweight.

The following figure illustrates the possible paths between the three loops and States. The colored elliptic represents the main state of the activity, and the rectangle represents the callback method that you can execute when the activity is converted between States.

Figure 1. Activity lifecycle (Source: Android SDK)

The following table describes each method in more detail and locates it throughout the lifecycle of an activity.

NoteKillableIt indicates whether the system can kill the active host process if no other code is executed when the method returns. The three methods (onpause (), onstop (), and ondestroy () are marked as yes. Because onpause () is the only one that can be called before the process is killed, you should use onpause () to write any persistent storage data.

Methods marked as no protect active host processes from being killed when they call them. Therefore, an activity is a kill State. For example, onpause () is returned to the onresume () call period. The activity cannot be killed until onpause () is returned again. Actually, it is not markedKillableActivities can also be killed by the system, but this only happens in extreme difficulties and there is no other resource available.

1. Passed through intent:

A. Character transfer: Set in activity1:

Java code

String text = "hello ";

Intent intent1 = new intent (activitymain. This, activity2.class );

Intent1.putextra ("activity1", text );

Startactivity (intent1 );

String text = "hello ";

Intent intent1 = new intent (activitymain. This, activity2.class );

Intent1.putextra ("activity1", text );

Startactivity (intent1 );

B. Upload objects. objects must be instantiated and serializable can be inherited.

Java code

Bundle mbundle = new bundle (); mbundle. putserializable ("user", userlist. Get (position ));

Intent in = new intent (getapplicationcontext (), activity2.class );

In. putextras (mbundle );

Startactivity (in );

Bundle mbundle = new bundle (); mbundle. putserializable ("user", userlist. Get (position ));

Intent in = new intent (getapplicationcontext (), activity2.class );

In. putextras (mbundle );

Startactivity (in );

Received in activity2:

A: receive

Java code

Bundle extras = getintent (). getextras ();

If (extras! = NULL ){

Textview. settext (extras. getstring ("activity1 "));

}

Bundle extras = getintent (). getextras ();

If (extras! = NULL ){

Textview. settext (extras. getstring ("activity1 "));

}

B. Receive

Java code

Bundle Bundel = getintent (). getextras ();

User = (User) Bundel. Get ("user ");

Bundle Bundel = getintent (). getextras ();

User = (User) Bundel. Get ("user ");

2. sharedpreferences

What I set in activity1 is as follows:

Java code

Sharedpreferences sp = getsharedpreferences ("textinfo", 0 );

Editor editor = sp. Edit ();

String text = "hello ";

Editor. putstring ("text", text );

Editor. Commit (); intent I = new intent (getapplicationcontext (), activity2.class );

Startactivity (I );

Sharedpreferences sp = getsharedpreferences ("textinfo", 0 );

Editor editor = sp. Edit ();

String text = "hello ";

Editor. putstring ("text", text );

Editor. Commit ();

Intent I = new intent (getapplicationcontext (), activity2.class );

Startactivity (I );

Jump to the Message activity and obtain the following content:

Java code

Sharedpreferences share = getsharedpreferences ("textinfo", 0 );

String text = share. getstring ("text", null );

Msgtextview. settext (text );

Android POSTMAN: Intent

The basic design concept of Android is to encourage the reduction of coupling between components. Therefore, Android provides intent (intent) and intent provides a general message system, it allows you to pass intent between your application and other applications to execute actions and generate events.

It is used to transmit data between multiple activities.

Invisible administrator of Android: Service

A service is literally a "service", which is similar to a service in windows.

The service runs in the background without an interactive interface.

The advantage of using service is that you can run multiple tasks at the same time, and the activity can only run one interactive interface.

The difference between startservice () and bindservice (): startservice (), the Service receives intent through oncreate () and onstart (), when a user sends an intent to destroy it, it will experience ondestory (), while bindservice () is started. When binding with activity, it will experience oncreate () and onbind () when the activity is destroyed, the service first calls onunbind () and then ondestory ().

Android receiver: broadcastreceiver

Broadcast receiver used to receive broadcast intent Asynchronously

The broadcast receiver does not have an interface, but can start an activity after receiving a message, or you can use the broadcast cationmanager to notify the user.

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.