First, what is activity?
Activity is simply an interface, and every interface we see on an Android phone is an activity.
Ii.creation of activity
1. Define a class to inherit activity, and then register the activity under the application node of the manifest file Manifest.xml file, and the activity is created successfully.
public class MyActivity extends Activity {}
2. inventory File Registration Activity
<application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@ String/app_name " android:theme=" @style/apptheme "> <activity android:name=" Com.example.study.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:name= "Com.yang.study.MyActivity" > </activity> </application>
Where Mainactivity is the program-initiated activity, which can be seen from the Intent-filter action
Our own definition of activity is the following myactivity.
<intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android: Name= "Android.intent.category.LAUNCHER"/> </intent-filter>
In particular: Each application can actually create multiple icons in launcher, which is the launcher of the Android phone, just configure the above intent-filter in the activity, and of course no one is doing it, Multiple app icons can be confusing to users.
Iii. Activation of activity
Now that the application has only one boot entry, how does the other activity start?
There are two ways to start activity, the first is to display the boot, and the second is to start implicitly
A.activity Display Start-up
Show startup is the class that you specify to start when you create the intent (intent)
1. Create Intent
2.startActivity (Intent)
Classes can be specified using the SetClass method
Intent Intent = new Intent () Intent.setclass (Mainactivity.this, Myactivity.class); startactivity (Intent);
or setclassname specifies the class name
Intent Intent = new Intent () Intent.setclassname (Mainactivity.this, "com.yang.study.MyActivity"); StartActivity ( Intent);
You can also directly construct the intent when you specify the class
Intent Intent = new Intent (mainactivity.this, Myactivity.class); startactivity (Intent);
All three of these forms can show the activation activity
Tips: Can you do this if you want to start the activity of other applications in one application?
The answer is yes, as long as the activated activity is set to true in the configuration of the manifest file, you can start it in other applications by doing the following:
Intent Intent = new Intent () intent.setclassname ("Com.yang.demo", "com.yang.demo.MainActivity"); StartActivity ( Intent);
Setclassname method The first parameter is the package name of the application to be started, and the second parameter is the class name to start
B.activity Implicit start-up
The implicit intent is to not specify the class to start at startup, but to start the activity by matching the action action with the data
eg. implicit start-up call interface
Intent Intent = new Intent () intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("tel://18888009988")); StartActivity (Intent);
To start the browser implicitly:
Intent Intent = new Intent () intent.setaction (Intent.action_view); Intent.setdata (Uri.parse ("http://www.baidu.com") );
<span style= "font-family:arial, Helvetica, Sans-serif;" >startactivity (Intent);</span>
The above two examples are the start system comes with some of the interface, about the system default interface, there are many, you can refer to the information on the Internet, due to space limitations, here is not listed.
If our activity is a browser tool, when we use the implicit launch browser above, we also want our activity to be launched, how to deal with it?
At this point, we simply set the Intent-filter intent filter in the avtivity manifest file, and when we use the implicit Start browser method above, our own defined activity will also be started.
Examples are as follows:
<activity android:name= "com.yang.study.MyActivity" android:exported= "true" > < intent-filter> <action android:name= "Android.intent.action.VIEW"/> <data android:scheme= " http "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-filter> </activity>
Setting the Intent-filter scheme parameter in the manifest file is limited to HTTP, so you can intercept the intent of all actions to start with HTTP Android.intent.action.VIEW data.
When we start the activity implicitly, our own activity is also activated.
Intent Intent = new Intent () intent.setaction (Intent.action_view); Intent.setdata (Uri.parse ("http://www.baidu.com") ); StartActivity (intent);
Iv. Display of activity
Activity is a user to see the interface, then how we define what to see, very simple, in the activity of the Oncreat () method, in which we can set the content we want to display
protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);}
The Setcontentview () method sets the interface that the user wants to display, placing the Ayout layout file to be displayed
V. Closure of activity
Acitivty close the Finish () method of calling activity directly to
protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Finish ();}
This article briefly introduces the activity of the start and close, the focus is the display and implicit two start-up mode, the actual use of the frequency is also very high, it is necessary to master.