How to create jumps and pass values for activity in Android development _android

Source: Internet
Author: User

There are four components in the Android system: activities, services (service), broadcast receivers (broadcast reciver), and content Provider.

Today's introduction is one of the four major components of Android development: Activity, the other three components to be introduced later. Speaking of activity in Android, if you've ever done iOS development, the activity is similar to the Viewcontroller (View Controller) in iOS . What you can see in your application is in the activity. Activity is an important thing in Android development, and it is the portal of user interaction and data. This blog to introduce the content is the creation of activities, activities and values of the jump through the transmission.

Viewcontroller in iOS also has its own lifecycle, and it is necessary to understand the activity or Viewcontroller lifecycle so you can figure out what to do at the time. Good nonsense, the activity will come on stage.

Using Android Studio to create an Android Add New activity project (step, see the previous blog), there is a blank activity by default in this project, and a text is automatically added to the blank activity View, which says "Hello World", runs the project and sees Hello World in a white event.

We have created a new activity in this project and then have a button in Hello World and click the button to jump to the new activity we created. Click Back in the New button to return to the previous activity.

I. Creation of activity and component additions

1. Create a blank activity

In the Android Studio file resource directory, select the package where you want to create the activity, right-click the->new-> activity-> Various activity, here we choose blank Activity, click it, The operation is shown in the following figure.

  

The following dialog box appears to create a new activity, which is our custom activity. Activity Name: Active name, Layout name: Activity corresponds to the name of the layout file, Titile: The name shown above for the navigation bar. Menu Resource Name: the names of the XML configuration files for the menus (described later in detail), click Finishi.

After the activity is created, it will generate three files in the resource directory, as shown in the following figure, the secondactivity file in Java has the preceding "C" flag, which is the Java class file, which is the source file of the activities. The activity_second.xml in the layout file is the corresponding layout file for the activity, in which you can specify which controls to add to the activity, and you can control the style and location of the controls. The third is the menu under the Menu_second.xml file, which is defined in the Navigation drop-down menu content, later to show you.

2. Control add

Adding a control to an activity requires manipulating the XML file for our layout folder. The next thing to do is to add a button to the layout file in Mainactivity, then get a button in the code, and listen for the click of the button, and the button click event Triggers a pop-up box. Take a detailed look at how to add controls and listen for events on the control.

(1) Add button

Open the Activity_main.xml layout file, switch to design mode, in the design mode you can drag and drop the way to create the control, and positioning the control. A button is added below, and the text on the button is Showtoast (toast is the component that displays the information in Android).

You can also switch to text mode to see the XML texts, and the big box below is the XML content of the button that we just dragged out, including information about the layout: the width of the control, the padding of the top and bottom, and so on, There are also some properties of the control: the ID that is unique to the control and the text that the control displays, and so on. Of course, if you know more about the attributes of XML layouts, you can use pure code to declare and layout your controls.

(2) Getting control in code

After the steps above, a button has been declared and configured. If you want to use the control in code again, you first have to instantiate the control by the ID of the button above. The following code is added in the Oncreat () method in the Mainactivity file, about the OnCreate method, and the lifecycle of later activity is described in detail. The controls specified in the layout layout file can be instantiated by Findviewbyid in Java source code. The ID of the button above is "Myfirstbutton", so the method to instantiate the button is as follows:

Get the buttons on the interface

After the button is instantiated, we need a click event for the listening buttons, and the following methods are used to resemble the block callback in iOS. is to give the button a click button to execute the method. Clicking the button will execute the callback method below, and it can be prompted by toast.

button-Clicked callback
Myfirstbutton.setonclicklistener (new View.onclicklistener () {
@Override public
void OnClick ( View v) {
toast.maketext (mainactivity.this, "click Myfirstbutton", Toast.length_long). Show ();

After the above steps, click on the Showtoast button will prompt "Click Myfirstbutton" Content, below is the results of the execution of screenshots:

The jump and transfer value between the activity

In the above we created a blank activity called secondactivity that has not been, from the above run effect can be seen in the mainactivity has a go Second activity button, She is used to jump to secondactivity, different activity see jump can pass a value, also can return a value, next to introduce the activity between the jump and transfer value problem.

1. Use of intent for activity jump

Intent is known as "intent" in Android development, and it's literally hard to understand, "Where are you going?" Intent is very similar to the Navigationcontroller (navigation Controller) in iOS development, but it is different. Navigationcontroller is a "stack" in the form of a container, the controller can push or pop operation to "stack" and "out of the stack", the stack and the operation of the stack is the view controller to switch operations. Intent implementation principle is the same, there is a stack, this stack is stored in the contents of a activity,activity start and finish operations corresponding to the stack of push and pop operations.

(1) in the "Go Second Activity" button click event to add jump code, jump to Secondactiviy, the code below, the code below is placed in the OnCreate method. In the constructor of the intent class used below, the first argument is the current activity, and the second is the activity that will jump. The StartActivity method to start the intent, rather than start the intent, rather than the current activity such as the stack, the will be shown in the stack.

Button Gosecondactivitybutton = (button) Findviewbyid (R.id.go_sceond_button);
Gosecondactivitybutton.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View V {
///Create a new Intent (current activity, secondactivity) ===== display Intent
Intent Intent = new Intent (Mainactivity.this, Secondactivity.class);
Start Intent
startactivity (intent);

(2) If the value in the mainactivity is passed to the secondactivity to jump past, the Putextra method in the intent object can be used to pass the value. Adding the following code above the Code startactivity () method is a value that is passed to the target activity. Data is a string variable with the value stored to be passed to the activity that will jump. Object intent by calling Putextra to pass the value, the first parameter is worth the name, that is the value of the key, in the next activity through the key to get the corresponding value.

Pass value to the next activity
String data = "I am the value passed in the last activity";

(3) In the new activity we need to get the passed value displayed on the TextView, and click on a Back button to return to the activity on the previous level, as shown in the code below. The code is placed in the OnCreate () method of the second activity. The getintent can be obtained through the intent jump, in other words to get the current navigation stack. After the intent object is fetched, a corresponding key is passed through the Getstringextra () method, which is used to get the value. Because the values we pass in are string types, so Getstringextra (), different types of values correspond to different methods. The obtained value is then displayed on the TextView. Then click the button to return. The finish () method in the button ends the current activity and automatically returns the previous activity. The TextView and button are also fetched by dragging, and then the event is processed and assigned by ID.

Gets the value of the last activity passed
Intent Intent = Getintent ();
String data = Intent.getstringextra ("Extra_data");
Displays the obtained value on the TextView
TextView Datatextview = (TextView) Findviewbyid (R.id.data_text_view);
Datatextview.settext (data);
Click button to return to the previous activity
Button Backbutton = (Button) Findviewbyid (R.id.bank_button);
Backbutton.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
Finish ();
}

After the above steps, the final operating effect is as follows, click the Go Second activity button in mainactivity to jump to the second activity, and the values passed in the first page will be displayed in the second activity. Clicking the Back button in the secondactivity will perform the finish () method to return to the previous activity.

2. Use intent to turn on system function

You can open some of the system's functions as a protocol, such as opening the system's browser, the system's dial-up keyboard, and so on. This is true of iOS, but it is only through the Application object to open some protocols such as tel://dial-up protocol to invoke system functions. These operations can also be done by intent objects in an Android system.

(1) Call the browser to open the link code as follows, the bottom code is the call browser to open the link. Action_view is more intelligent, he will be through the user incoming data to open the corresponding application, the bottom is through the SetData incoming URL, so will call the browser, if the incoming tel:10010, will call the dial.

Button Openbaidubutton = (button) Findviewbyid (R.id.open_baidu_button);
Openbaidubutton.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
Call the local browser to open the URL
Intent Intent = new Intent (intent.action_view);
Intent.setdata (Uri.parse ("http://www.baidu.com"));
StartActivity (intent);
}

(2) Call dial you can change the incoming data by the way above, but you can also call dial by intent.action_dial, the specific code is as follows:

Button Telphonebutton = (button) Findviewbyid (r.id.tel_number);
Telphonebutton.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
Intent Intent = new Intent (intent.action_dial);
Intent.setdata (Uri.parse ("Tel:"));
StartActivity (intent);
}

Click on the top two buttons the first one will open the URL through the browser, the second will open the dial, the effect is as follows.

3. Get the value from the activity in return

From the above example, we can pass the value from one activity to the next activity. The next thing to do is to get the value from the returned page, just the opposite. This transfer value is also done through intent. We add a button to the mainacvitiy "go Third activity", click the button to jump to the third activity, and then return to get the values passed in the third activity.

(1) When the jump through the Startactivityforresult () method to be worthy of return, the first parameter is intent object, the second parameter is Requestcode (request code). Requestcode is used in the callback method that is returned.

Button Gothiredactivitybutton = (button) Findviewbyid (R.id.go_third_button);
Gothiredactivitybutton.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View V {
//Get data from the next activity
Intent Intent = new Intent (mainactivity.this, thirdactivity.class);
Startactivityforresult (Intent, 1);
}
});

(2) in the thirdactivity to do is to return the value by intent, the specific code as shown below, the return value is also through the intent object Putextra method, and then to execute the Setreault method. The first parameter of the Setresult () method is ResultCode (result code) and is used in callbacks that receive values in the previous activity.

Button Backbutton = (button) Findviewbyid (R.id.third_back_button);
Backbutton.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
Intent Intent = Getintent ();
Intent.putextra ("Data_return", "I am the data returned in the third activity");
Setresult (RESULT_OK, intent);
Finish ();
}
});

(3) Then you have to rewrite the callback method that handles the return value in the mainactivity, that is, the Onactivityresult callback method. In the method, the key is used to get the passed value, and the value is assigned to the TextView code in the mainactivity as follows:

@Override
protected void onactivityresult (int requestcode, int resultcode, Intent data) {
switch (requestcode) { Case
:
if (ResultCode = = RESULT_OK) {
String returneddata = Data.getstringextra ("Data_return");
TextView Returneddatatextview = (TextView) Findviewbyid (R.id.return_textview);
Returneddatatextview.settext (Returneddata);
}
break;
}
}

After the above steps, the effect is as follows, click Go Third the activity will jump to thirdactivity, Then clicking the Back button from the thirdactivity displays the value returned in the thirdactivity in the TextView on the previous activity, as shown in the following example.

The above is a small set to introduce the Android development activity to create jump and the value of the method of the relevant knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.