2016-06-06 Android Activity

Source: Internet
Author: User

1.Activity lable refers to the activity's name, if the activity also specifies: The following intentfilter, then the interface will be created in the desktop shortcut, the shortcut name is the activity of the lable< Intent-filter><action android:name="Android.intent.action.MAIN"/><category android:name="Android.intent.category.LAUNCHER"/></intent-filter>category: Specifies the category of intent, launcher is to create a shortcut on the desktop. Intent-filter child nodes: Add intent filtering, which can be initiated by implicit intent. You can generate shortcuts on the desktop, the application portal. Note: Too much data passed by intent can result in very slow jump speeds or even a black screen, and do not use intent to pass too much data, which can affect the use of your application.# #1. Creation and destruction of activity (*)1.Manifest file content: Phone browser manifest file content permissions <uses-permission android:name="Android.permission.CALL_PHONE"/> <uses-permission android:name="Android.permission.INTERNET"/><intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/></intent-filter><activity android:name="Com.guyulei.activity2.MyBrowserActivity"Android:label="Zhou Hongwei Browser"android:icon="@drawable/xiaowei">category: Type, department, kind, category, class; [Logos, ZHE] category;   Weight level scheme: planning, plotting; Prefix filter: filter; Optical filters; Filter            [Filter]; <intent-filter > <action android:name="Android.intent.action.VIEW"/> <category android:name="Android.intent.category.DEFAULT"/> <category android:name="Android.intent.category.APP_BROWSER"/> <data android:scheme="http"/> <data android:scheme="https"/> <data android:scheme="About"/> <data android:scheme="JavaScript"/> </intent-filter> </activity> Summary: Each class corresponds to its own activity binding its own XML file activity registers an implicit jump in the manifest (implicit intent Jump) (low efficiency) is used to jump the activity between different apps, by matching the target activity's intent-filter to achieve the jump intent filter matching rules1.If an activity has multiple intent-filter, then we just need to match any one of them.2.If you have action, category, data in Intent-filter, you must match all3.If category is <category android:name="Android.intent.category.DEFAULT"/> scenario, you can not match.4.If you have more than one category, you can take any one of the matches5.If data has more than one, then take any one of the matches6.If data has two, and the attribute is not the same, then the two properties of this data must all match mainactivity: implicit jump: Call Intent.action_call Public void Callphone(View view) {//2Intent Intent =NewIntent ();//3Intent.setaction (Intent.action_call);//4Intent.addcategory (Intent.category_default);//default can not write        //5Intent.setdata (Uri.parse ("tel:15958121433"));//1StartActivity (Intent); } Implicit jump: Browser Intent.action_view Public void Callbrowser(View view) {//2Intent Intent =NewIntent ();//3Intent.setaction (Intent.action_view);//4Intent.addcategory (Intent.category_default);//5Intent.setdata (Uri.parse ("Http://www.baidu.com"));//1StartActivity (Intent); The implicit jump premise between different projects needs to know information about activity in the manifest file Intent.setdataandtype format MIME messages that cannot coexist with data and type can contain text, images, audio, Video and other application-specific data. Type/subtype manifest file: attached: List of the second project data Android:mimetype <activity android:name="Com.guyulei.activity02.MyActivity"> <intent-filter > <action android:name="Com.guyulei.zhonghongwei.TEST"/>//You can set the same configuration when you jump<category android:name="Android.intent.category.DEFAULT"/> <data android:scheme="Guyulei2zhouhongwei"/>//Data format prefix at transfer +:<data android:mimetype="Gu/zhou"/>//type of data</intent-filter> </activity>/ * * Jump between different project activity */     Public void Skip2guyulei(View view) {Intent Intent =NewIntent ();//setaction corresponds to the action of the activity in the manifest file.Intent.setaction ("Com.guyulei.zhonghongwei.TEST"); Intent.addcategory ("Android.intent.category.DEFAULT");/* * SetData setType not co-exist * use Setdataandtype to resolve * Intent.setdata (Uri.parse ("Guyulei2zhouhongwe           I: Zhou Xiaowei "));         Intent.settype ("Gu/zhou"); */Intent.setdataandtype (Uri.parse ("Guyulei2zhouhongwei: Zhou Xiaowei"),"Gu/zhou");    StartActivity (Intent); }mybrowseractivity: Get Intent data, intent start this activity, this activity get intent data intent data from where? Format the data in the list add data when you start intent//intent.setdata (Uri.parse ("http://www.baidu.com"));Getintent () intent.getdatastring ();protected void onCreate(Bundle savedinstancestate)        {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_my);//Get who initiated the activity of the intent, data within intent        //intent.setdata (Uri.parse ("http://www.baidu.com"));Intent Intent = Getintent ();        String datastring = intent.getdatastring ();        LOG.D (TAG, datastring); Toast.maketext ( This,"The page you are viewing is:"+datastring, Toast.length_short). Show (); The second project myactivity the first project intent came to the second project to bring in the second project format data intent.setdata (Uri.parse ("Guyulei2zhouhongwei: Zhou Xiaowei")); Intent.setdataandtype (Uri.parse ("Guyulei2zhouhongwei: Zhou Xiaowei"),"Gu/zhou");protected void onCreate(Bundle savedinstancestate)        {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Get data encapsulated in intent within the Zhou Xiaowei activity        //data and type separate takeIntent Intent = Getintent ();        Uri data = Intent.getdata ();        String type = Intent.gettype (); LOG.D (TAG,"Data="+data+"....."+"Type="+type); Toast.maketext ( This,"Data:"+data+"type:"+type, Toast.length_short). Show (); }2.Explicit jumps are often used in engineering internal display jumps (explicit jumps) (high efficiency) directly through the target activity of the bytecode for the jump, more used in the app between different activity. If the activity only wants to be shown a jump, then there is no need to configure Intent-filter. Jump Code StartActivity (NewIntent ( This, Secondactivity.class)); manifest file Registration activity<activity android:name="Com.guyulei.activity03.MySecond"></activity>mainactivity:intent.setclass ( This, Mysecond.class); Intent.putextra ("Name","Gu Yulei"); android:text="I was the first activity to jump to second." Public void Skip2second(View view) {Intent Intent =NewIntent (); Intent.setclass ( This, Mysecond.class);//Show jump to second data transferIntent.putextra ("Name","Gu Yulei"); Intent.putextra ("Age", -); Intent.putextra ("Lover","Zhou Xiaowei");    StartActivity (Intent); }mysecond: Take data: Take the data first with intent intent getintent () Intent.getstringextra ("Name");protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stubSuper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second);/ * * Show delivery data take the data first with intent intent * /Intent Intent = Getintent (); String name = Intent.getstringextra ("Name");intAge = Intent.getintextra ("Age",0); String lover = Intent.getstringextra ("Lover"); LOG.D (TAG,"Name="+ name +"Age="+ age+"Lover="+lover); Toast.maketext ( This,"The message is:"+"Name="+ name +"Age="+ age+"Lover="+lover, Toast.length_long). Show (); }}mainactivity:startactivityforresult (Intent, Requestcode); Jump to secondrequestcodeandroid:text= in a way that needs to get results"Jump to SEC and get its return value"android:onclick="Skip2secondforresult"//Jump to second and get the return value when it is destroyed     Public void Skip2secondforresult(View view) {Intent Intent =NewIntent (); Intent.setclass ( This, Mysecond.class);    Startactivityforresult (Intent, Requestcode); }mysecond:android:text="return data to Mainactivity and destroy yourself"Resultcoe Setresult (Resultcoe, data);//Who initiates me, I will return the data to who, and destroy myself     Public void  Back(View view) {Intent data =NewIntent ();//Package data for returnData.putextra ("Data","This is the data returned to you by Secondactivity");        Setresult (Resultcoe, data);    Finish (); }mainactivity:onactivityresult//Call callback function to get the data returned by second@Overrideprotected void Onactivityresult(intRequestcode,intResultCode, Intent data) {Super.onactivityresult (Requestcode, ResultCode, data); LOG.D (TAG,"Requestcode="+requestcode+"Resultcode="+resultcode+"Data="+data);if(Requestcode==requestcode) {if(resultcode==1){//Get DataString result = Data.getstringextra ("Data"); LOG.D (TAG,"result="+result); Toast.maketext ( This,"The data returned when the second is destroyed is:"+result, Toast.length_long). Show (); }        }    }3.What are the types of data that intent can carry?1. 8Type of basic data and its corresponding array form2.String and its array3.Objects that implement the Serializable interface4.Objects that implement the Parcelable interface4.The difference between serializable and parcelable?1.The former is Java, the latter is Google's own development2.The former is less efficient and the latter more efficient, specifically for serialization in Android systems3.The usage is completely different, the former is very simple, the latter is complex.5.When the activity jumps, it passes the data and gets1.Intent.putxxx ();2.Getintent (). GETXXXEXTR ("Key", DefaultValue);6.Returns data when a jump to activity is destroyed (must be)1.In Mainactivity, when starting secondactivity, you cannot use StartActivity (), use Startactivityforresult (intent, Requestcode)2.In Secondactivity, before finish, call Setresult (responsecode,intent data); method sets the data to be returned, then the finish ()3.Overwrite Onactivityresult () in mainactivity to get the data secondactivity set in the function# #3. Life cycle of activity (* * * * *)# # #3.1 three states of activity1.Resumed (running running state): If the activity is at the forefront of the screen and gets the focus of the user.1.Fully visible and interoperable.2.Will not be killed by the system until oom, then crashes.2.Paused (paused state): covered by another activity, but still partially visible (transparent, or comparative).1.Partially visible, non-interactive.2.When the system content and its shortage, it will be killed by the system.3.Stopped (Stop State): If the current activity is completely covered by another activity (background activity).1.Completely invisible, more non-interactive2.When the system needs to release memory, it will kill.# # #3.2 Life cycle Methods1.OnCreate2.OnStart3.Onresume4.OnPause5.OnStop6.OnDestroy7.Onrestart <activity android:screenorientation="Reverselandscape"android:configchanges="Orientation|keyboardhidden|screensize"Android:name=". Mainactivity "Android:label="@string/app_name"> <intent-filter> <action android:name="Android.intent.action.MAIN"/> <category android:name="Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:theme="@android: Style/theme.translucent.notitlebar.fullscreen"Android:name="Com.guyulei.activity04.MySecond"></activity> make Activity A dialog style configure the following parameters for the specified activity in the manifest file: android:theme="@android: Style/theme.dialog"Make activity transparent. Configure the following parameters for the specified activity in the manifest file: android:theme="@android: Style/theme.translucent"# #4 life cycle when switching between the screenSimulator Toggle: (Fn) +control+f11 by default, the current activity is completely destroyed and recreated. Keep the activity on the screen without changing his life cycle in the manifest file, configure the following properties android:configchanges="Orientation|keyboardhidden|screensize"Fixed activity direction through configuration file1.android:screenorientation="Landscape"Level2.android:screenorientation="Portrait"Vertical through code1.Setrequestedorientation (Activityinfo.screen_orientation_landscape);2.Who after configuring who takes effect# #5. Activation mode of activity (* * *)Activity is managed by the Activitymanager stack structure.# # #5.1 activity in four startup modes1.Standard (also the default, if not Configured is standard):1.Features: A fool-like, every activity started, re-create a new activity, and then pressed to the top of the stack.2.Singletop1.Feature: If the activity of Singletop configured at the top of the current stack already exists, it will not be recreated.3.Singletask1.Features: Ensure that the current stack has only one instance of activity, and if there are other activity to hold it down, he wants the top to be the way to stack all other activity.4.SingleInstance1.Features: A task stack for a single activity.

2016-06-06 Android 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.