Android Basic Learning the second article-activity

Source: Internet
Author: User

written in the previous words:

1. Recently in self-study Android, is also reading while writing some demo, because of more and more knowledge points, the brain is more and more memory is not clear, so intends to write reading notes, for later review, but also to learn what they understand to write out, caught dead, if there is a wrong place, I hope everyone give and correct.

2. Because similar to reading notes, may format God horse will compare with (hen) meaning (chou), we can't stand, you can hard spit groove.

I'm just dividing the line ***************************************

The previous article has already talked about something about the project directory structure , this time documenting something about the activity of one of Android's four components, which is one of the most basic modules in Android.

< a > What is activity?

It's called activity (sounds awkward, so long it feels bad to accept ~), almost all activities are used to interact with the user, the activity is mainly concerned with the creation of the view form, an application can contain 0 or more activities, but there are fewer applications with 0 activities. PS: This is a reference to others, I just take to install X "

In fact, we can think of the mobile phone as a browser, and activity is one of the pages, in activity, we can add a different view (that is, the control in. net), and can perform some operational logic.

< two >activity life cycle

"Pictures are borrowed from others",

The "Entire life cycle" of an activity occurs between the first call to OnCreate (Bundle) and the only last call to the OnDestroy () method. The initialization of activities completed in the OnCreate (Bundle) method sets the global state, such as loading layouts, binding events, and freeing resources in the OnDestroy () method.

The activity's "display life cycle" occurs between calling the OnStart () method and calling the corresponding OnStop () method. In between, you can see this activity on your phone screen. These two methods can be called multiple times as the activity changes the visible and invisible state of the user.

The activity's "foreground life cycle" is experienced between the Onresume () and the OnPause () methods, during which the activity is running, and the activities can interact with the user at this time.

My personal approach is to create an app and rewrite all of these methods in the activity's code, make a breakpoint (or log output) for each method, debug debugging, and basically understand when it will be called.

< three >activity data transfer between start-up and activity

Here we need to use an object intent,intent is an important way to interact between components in an Android program, and it can be used to launch events, start services, and send broadcasts, in detail, later on.

1. Simply start a new activity

Method of Use: StartActivity (Intent)

Example:

New Intent (); _intent.setclass (mainactivity. this, activityb. class ); startactivity (_intent);

This invocation method can also be written as

Intent _intent2 = new Intent (mainactivity.this,activityb.class);
StartActivity (_intent2);

Instantiate a intent object, and then specify the activity from the current mainactivity to jump to activityb.

2. Start a new activity with a return value

How to use: public void Startactivityforresult (Intent Intent, int requestcode), note the second parameter: Requescode, this parameter is similar to the secret code of the original agent connector,

Example:

1. The application jumps from the active mainactivity to the ACTIVITYC and requests a string;

2. In active activity, when closing the activity ACTIVITYC, return the requested string to mainactivty;

3. Activity mainactivity Gets the string.

    class Implements Onclicklistener {        @Override        publicvoid  OnClick (View v) {             if (V.getid () = = R.id.btnskiptoc)             {new  Intent ();             _intent.setclass (mainactivity. this, activityc. class );                          }}}    

Note that this is the start of the new activity ACTIVITYC, the password Requestcode is 100.

In the active ACTIVITYC, when you click the Close button, the requested string is returned with void Setresult (int resultcode, Intent data).

    classButtonactivityalistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {if(V.getid () = =r.id.btnreturn) {Intent _intent=NewIntent (); EditText EditText=(EditText) Findviewbyid (R.id.edittext_first); String Inputstr=Edittext.gettext (). toString (); _intent.putextra ("Dashenzaijia", INPUTSTR);                Setresult (ACTIVITY.RESULT_OK, _intent);            Finish (); }        }    }

Note: Intent.putextra (string name, string value) is similar to a data dictionary, this Intent is ACTIVITYC return string to mainactivity vector, name is the key, finish () method is similar to close () in WinForm;

After you close the active ACTIVITYC, and then mainactivity, there is a virtual method, overload it, and then get the return value of ACTIVITYC from this method, as follows:

@Overrideprotected voidOnactivityresult (intARG0,intarg1, Intent arg2) {        //TODO auto-generated Method Stub        Super. Onactivityresult (arg0, arg1, arg2); if(arg0 = = && Arg1 = =ACTIVITY.RESULT_OK) {String returnvalue= Arg2.getstringextra ("Dashenzaijia"); TextView View= (TextView) This. Findviewbyid (R.id.textview_showresult); if(!Returnvalue.isempty ()) {View.settext ("Value from activity C:" +returnvalue); } Else{View.settext (NULL); }        }    }

The first parameter is the password between the two activities that was originally defined, the second parameter is the result when the page closes after the jump, similar to the DialogResult in C # when the form is closed, and the third parameter is the carrier of the return value.

The most basic knowledge about activity is finished.

Android Basic Learning the second article-activity

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.