Android Create simple Interactive page

Source: Internet
Author: User

1. Activity focuses on the creation of a view form (a Web page that is currently understood as a Web site). It has the concept of life cycle, Oncreate,onstart,onrestart, Onresume,onpause,onstop,ondestroy. OnStart () and OnStop () can be called multiple times as Activity changes to the visible and invisible state of the user.

  • Between the display life cycle OnStart <---> onstop, the user can see activity on the screen, although it may not be in front (hiding the transparent activity overlay, etc.) from interacting with the user. Between the two methods, you can maintain the resources you need to display to the user.
  • Between the "foreground life cycle" Onresume <--->onpause, activity is displayed on the screen and the user interacts with it. Activity is often converted between recovery and pause. For example, when the multi-meter music is playing, the music pauses while the machine is hibernating, and the music continues to wake up.
  • Load pattern (Launch mode): First, a task is a collection of activities (already instantiated) that interact with the user and perform a specific work. The "instances" of these activity are arranged in the stack according to the order in which they are started.
      • Standard mode: By default, a new activity instance can be created in a task, and the activity can be instantiated multiple times, task (many)-activity instances (many);
      • Singletop mode: If an activity instance already exists at the top of the current task, the system calls this instance by calling its onnewintent to send a intent request instead of creating a new instance. Task (Multi)-activity instance (multi) [Note: The difference from standard is that while a task can have multiple instances, when an instance is created, it is only possible if the top of the stack is not an instance where the activity already exists, otherwise it is simply called];
      • singletask mode: In short, even if it is not top, as long as a task has this instance, it does not create a new instance of the activity for any task. Even if the activity initiates a new task, the return is returned to the previous activity. at the same time, there is only one instance of activity.
      • singeinstance mode: Same as Singletask mode, but activity instance has only one and it is the only member of the task. Other activity that is then initiated by the activity will be started in a different task.
      • note: By using flag in intent, you can also break the default behavior effect. Set the intent value to Flag_activity_new_task, Flag_activityy_single_top, Flag_activity_clear_top.

2. Configuration changes: Unless specified, if the configuration of the device changes (defined in the Resource.configuration Class), any display on the interface needs to be updated to accommodate the new configuration (screen orientation, language, input device). The activity is destroyed by default and the corresponding activity life cycle function Onpause,onstop,ondestroy is called. You can configure the Android:configchanges property in the manifest file if you want to bypass the restart of the activity to deal with configuration changes. Any configuration type defined in manifest will callback the current activity's onconfigurationchanged method instead of restarting the activity.

3. Start activity and get results: In activity, you can call StartActivity (Intent)/startactivityforresult (intent,int) [Results via Onactivityresult (int,int,intent) Get "use Setresult (Activity.result_ok,data) in callee;//data.putextra (" Huan ", Val); Passed to the caller "Use the intent data" method to enable a new Activity.

Android.content.Intent.Intent (Context packagecontext, class<?> cls)

Create an intent for a specific component. All other fields (action, data, type, Class) is null, though they can is modified later with explicit calls. This provides a convenient-to-create a intent that's intended to execute a hard-coded class name, rather than Relyin G on the system to find a appropriate class for you; See for more information on the repercussions of this setComponent .

Parameters:
packagecontext A Context of the application package implementing this class.
the
CLS -Component class, is-is-used for the intent.

For example:

1Mbutton2.setonclicklistener (NewOnclicklistener () {2 @Override3           Public voidOnClick (View v)4          {5Intent _intent =NewIntent (mainactivity. This, Activityc.class);6Startactivityforresult (_intent,100);7           //The first one is the intent object, and the second is the request code, which distinguishes the different requests8          }9});
1  Public classActivitycextendsActivity {2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (R.LAYOUT.ACTIVITYC);5         6Button button1 =(Button) Findviewbyid (r.id.buttionc);7         8Button1.setonclicklistener (NewOnclicklistener () {9 @OverrideTen              Public voidOnClick (View v) { OneIntent data =NewIntent (); //visible, intent can be constructed in many ways, either. PutExtra pass <key,value> value, or as a bridge to start new Activity.  AEditText et =(EditText) Findviewbyid (R.ID.TEXTACTIVITYC); -String val =Et.gettext (). toString (); -Data.putextra ("Huan", Val); the Setresult (activity.result_ok,data); - finish (); -             } -         });  +     } -}

4. It is important to note that when starting different activity, the Requestcode should be set individually so that the Onactivityresult () can get different intent content, thus choosing the view on this page to do different processing.

void Android.app.Activity.startActivityForResult (Intent Intent, int requestcode) same as calling With startActivityForResult(Intent, int, Bundle) no options.

Parameters:
Intent the intent to start.
requestcode If >= 0, this code would be returned in Onactivityresult () when the activity exits.
1Mbutton1.setonclicklistener (NewOnclicklistener () {2 @Override3           Public voidOnClick (View v)4          {5Intent _intent =NewIntent (mainactivity. This, Activityb.class);6Startactivityforresult (_intent,200);7          }8});

1 @Override2     protected voidOnactivityresult (intRequestcode,intresultcode,intent data) {3         Super. Onactivityresult (Requestcode, ResultCode, data);4         if(Requestcode = = && ResultCode = =ACTIVITY.RESULT_OK) {5String val = Data.getextras (). getString ("Huan");6TextView TV =(TextView) Findviewbyid (r.id.tvdisplay);7Tv.settext ("Value from ACTITIVITYC:" +val);8         }9         if(Requestcode = = && ResultCode = =ACTIVITY.RESULT_OK) {TenString val = Data.getextras (). getString ("Xin"); OneTextView TV =(TextView) Findviewbyid (r.id.tvdisplay); ATv.settext ("Value from Actitivityb:" +val); -         } -         /* the * The Activityresult on this page, regardless of which button jumps, need to be based on the requestcode of their new activity to judge.  - * There is no distinction between the same requestcode of different activity.  -          */ -          +}

5. All Activity must be registered in the Androidmanifest.xml file: <activity android:name= ". Activityc "/>

 

Android Create simple Interactive page

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.