The 2nd chapter starts from the view, explores the activity Knowledge point 1: Use the menu in the activity
-
Create a new menu folder in the Res directory, right-click the menu folder →new→android XML file, and enter the file name in main
-
Add the following code to the Main.xml:
<menu xmlns:android= "http://schemas.android.com/apk/res/android" ><itemandroid:id= "@+id/add_item" Android:title= "Add"/><itemandroid:id= "@+id/remove_item" android:title= "Remove"/></menu>
-
Overriding the Oncreateoptionsmenu () method in the code (through the Getmenuinflater () method to get the Menuinflater object, and then calling its inflate () method, can give
The current activity creates the menu. )
public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;}
-
Overriding the Onoptionsitemselected () method in code
public boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {Case R.id.add_item:toast.maketext ( This, "You clicked Add", Toast.length_short). Show (); Break;case R.id.remove_item:toast.maketext (This, "you clicked Remove ", Toast.length_short). Show (); Break;default:}return true;}
Knowledge Point 2:intent
-
Show intent (slightly)
-
Implicit intent
-
By configuring <intent-filter> Under the <activity> tab, you can specify the action that the current activity responds to
and category (only the contents of <action> and <category> can match the action and category specified in Intent, the activity should Intent)
<activity android:name= ". Secondactivity "><intent-filter><action android:name=" Com.example.activitytest.ACTION_START "/> <category android:name= "Android.intent.category.DEFAULT"/></intent-filter></activity>
-
To add a response event to your code
Button1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (" Com.example.activitytest.ACTION_START "); StartActivity (intent);});
-
Note: Only one action can be specified in each intent, but it is possible to specify multiple category.
We can add custom category in the manifest file (<category android:name= "Com.example.activitytest.MY_CATEGORY "/> ;)
The code is added (Intent.addcategory ("Com.example.activitytest.MY_CATEGORY");)
-
More implicit Intent usage (call the system's browser to open this page)
Button1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent ( Intent.action_view); Intent.setdata (Uri.parse ("http://www.baidu.com")); StartActivity (Intent);}});
Note: We can also configure a <data> tag in the <intent-filter> tab to more precisely specify what type of data the current activity responds to. The following can be configured primarily in the <data> tab.
-
Return data to upper activity
-
Interface 1:
Intent Intent = new Intent (firstactivity.this, Secondactivity.class); Startactivityforresult (Intent, 1);
-
Interface 2:
Intent Intent = new Intent () Intent.putextra ("Data_return", "Hello firstactivity"); Setresult (RESULT_OK, Intent); Finish ();
-
public void onbackpressed () {Intent Intent = new Intent (); Intent.putextra ("Data_return", "Hello firstactivity"); Setresult (RESULT_OK, intent); Finish ();}
-
Interface 1:
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (requestcode) {case 1:if (ResultCode = = RESULT_OK) {String returneddata = Data.getstringextra ("Data_return"); LOG.D ("Firstactivity", Returneddata);} Break;default:}}
Knowledge Point 3:dialogactivity
-
New dialogactivity inherit from Activity
-
Modify the manifest parameter to <activity android:name= ". Dialogactivity "Android:theme=" @android: Style/theme.dialog ">
Knowledge point 4: Save data with Savedinstancestate
-
protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstancestate (outstate); String TempData = "Something you just typed"; Outstate.putstring ("Data_key", TempData);}
-
protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.D (TAG, "onCreate"); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); if ( Savedinstancestate = null) {String TempData = savedinstancestate.getstring ("Data_key"); LOG.D (TAG, tempdata);} ......}
Knowledge Point 5: Active startup mode (you can select the startup mode by assigning the Android:launchmode property to the <activity> tag in androidmanifest.xml)
-
Standard: Create an instance each time you start (system default)
-
Singletop: When the active startup mode is specified as Singletop, if it is found that the stack top of the return stack is already active at the start of the activity, it is considered that it can be used directly and no new active instances can be created (to prevent repeated creation of instances on successive clicks)
-
Singletask: Only one instance can exist. Not new unless destroyed
-
SingleInstance: Manages activities in a single return stack, sharing a return stack regardless of which app accesses the activity
Knowledge Point 6: Destroying multiple activities at once
-
New Activitycollector class to manage so activity
public class activitycollector {public static list<activity> activities = new ArrayList<Activity> ();p ublic static void addactivity (activity Activity) {activities.add (activity);} Public static void removeactivity (activity activity) {activities.remove (Activity);} Public static void finishall () {for (activity activity : activities) {if (!activity.isfinishing ()) {activity.finish ();}}}
-
To modify the code in Baseactivity
public class Baseactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.D ("Baseactivity", GetClass (). Getsimplename ()); Activitycollector.addactivity (this);} @Overrideprotected void OnDestroy () {Super.ondestroy (); Activitycollector.removeactivity (this);}}
-
When you need to complete all activity, just call Activitycollector.finishall ();
Knowledge Point 6: Best Practices for initiating activities (prevent missing parameters when calling)
-
public class Secondactivity extends baseactivity {public static void ACTIONStart (context context, string data1, String dat A2) {Intent Intent = new Intent (context, secondactivity.class); Intent.putextra ("param1", data1); Intent.putextra (" Param2 ", data2); context.startactivity (intent);} ......}
-
When called: Secondactivity.actionstart (Firstactivity.this, "data1", "data2");
This article from "JESSE0 Blog" blog, reproduced please contact the author!
"First line of code" study Note 2nd Chapter