Switching between Android's basic learning activity

Source: Internet
Author: User

First of all, I'd like to introduce intent this guy, yes, that's the guy who let us do the jump switch between the activity. The following introduction refers to some of the things that the enjoy wind chimes wrote about "intent":

Intent is a lively little boy in the Android family, and has been a sociable grass since childhood. In the code, the activity, Service, broadcastreceiver the three important Aunt weight components, the call association between the intent to communicate, such as activity of StartActivity (), Service StartService (), Bindservice (), Broadcastreceiver sendbroadcast (), sendorderbroadcast (), etc., All of which are required to carry a intent (or intentfilter) parameter.

Of course intent to specify the information to start the component, or obviously live in a vague. The obvious is the explicit start-up: For example, intent you to give this thing to XXX (name) activity aunt. Obscure is the implicit start: Of course, it is inevitable to give an example is intent you put this to last night and I together in the market jump square dance of that fat aunt, which square dance and fat put an aunt to the red fruit of the position. This is intent's two ways of matching, I think I said the strange abstract.

The next is a good time to introduce Intentfilter, which is the son of Aunt (module Interceptor). When intent implicitly starts, intent will find his group of friends Intentfilter, and then ask them, "Who's mom went to the market and danced in the square last night?" Who's your mom is so fat? Then Intentfilter each mother, oops, one of the intentfilter Pat Head said, this is not my mother! Then he went to the intent of the fart to find some activity mom to give her.

In order to prevent people from reading the above example, I still carry forward the social responsibility of 2B youth, introduce the intent and intentfilter in a more dogmatic language:Intent is an object of intent, and the component can start another component by setting the intent. There are two methods, explicit start and implicit startup, which explicitly start specifying the name of the component to start, implicitly start specifying action, category, data, and so on to launch the eligible component; The Intnetfilter is an intent filter. It sets some filtering rules, such as action, category, data, and so on, if the action, category, and data in the intent match the filter, the component that launches the filter.

The next step is to show you a simple jump process and code:

The first is to create an Android project, project name customization, I use Intent_app as the project name.

Next, add a button to the XML page of the first activity, and the ID is button1 by default;

Add 2 methods to the Mainactivity.java file that corresponds to the XML page: (post code later)

Findviewbyid (): Used to create a button object.

Setlistener (): Used to create an event listener.

Create a App_intent.xml page that you can use to jump. Create a new App_intentactivity.java file and add the OnCreate () event inside the file.

Above is a simple page jump process, little brother, write very unfair, plus the expression of knowledge point is very basic, but also look for crossing Haihan. The code is as follows:

1  PackageCom.example.intent_app;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 ImportAndroid.view.Menu;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 Importandroid.content.Intent;Ten  Public classMainactivityextendsActivity { One  A     PrivateButton MyLogin; - @Override -     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - Findviewbyid (); - Setlistener (); +     } -      + @Override A      Public BooleanOncreateoptionsmenu (Menu menu) { at         //inflate the menu; This adds items to the action bar if it is present. - getmenuinflater (). Inflate (R.menu.main, menu); -         return true; -     } -      Public voidFindviewbyid () -     { inMyLogin =(Button) Findviewbyid (R.id.button1);  -     } to      +      Public voidSetlistener () -     { theMylogin.setonclicklistener (NewOnclicklistener () { *              Public voidOnClick (View v) { $StartActivity (NewIntent (mainactivity. This, App_intentactivity.class));Panax Notoginseng finish (); -             } the         }); +     } A}
1  PackageCom.example.intent_app;2 3 ImportAndroid.os.Bundle;4 Importandroid.app.Activity;5 ImportAndroid.view.Menu;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 Importandroid.content.Intent;Ten  One  Public classApp_intentactivityextendsActivity { A     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.app_intent); the     } -}

Basically no technical content, as a novice I still wrote it down (because afraid of forgetting) ...

PS: Every time you create an activity remember to register, I just forgot to register this stubble, adjusted me for two hours (English level of the landlord but 2, do not despise ~ ~ ~ ~ ~)

Added in Androidmanifest.xml:

1   <activity2      android:name= "The name of the activity to be registered, such as Com.example.intent_app. App_intentactivity "3      android:screenorientation=" Portrait ">4   </activity>

Switching between Android-based learning 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.