Android Intent for page Jump

What is Intent

Intent can be understood as a messenger (intention)

Intent cooperates to complete the communication between Android components, or to realize the jump between pages

Intent to jump between pages

  1. startActivity(intent) / / the first way to start
  2. startActivityForResult(intent, requestCode); / / the second way to start

    onActivityResult(int requestCode, int resultCode, Intent data)

    setResult(resultCode, data);

The first startup method realizes direct jump without return value

The second startup mode is page A - > page B, and page B can also send back data to page A

onActivityResult(int requestCode, int resultCode, Intent data)

This is for A page to receive data returned from B page

setResult(resultCode, data) this is used to return data from page B to page A

Create a project first

Create two new activities

 

Then create two new page layouts in layout. Right click layout to create new Android XML File

Then, bind, for example:

Then, configure the manifest file AndroidManifest.xml

Then, insert two button s and a textview in activity "first" for the first jump mode and the second jump mode, as well as data return. The code is as follows:

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6 
 7     <Button
 8         android:id="@+id/bt_first"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:text="First startup mode" />
12 
13     <Button
14         android:id="@+id/bt_Second"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:text="Second startup mode" />
18 
19     <TextView
20         android:id="@+id/textview"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="Display the data returned from the second page" />
24 
25 </LinearLayout>

As mentioned just now, I need to use Intent intention to realize jump. The code is very simple and clear. I posted the code directly

First, FirstActivity

 1 package com.example.intentdemo;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class FirstActivity extends Activity {
12     Button bt_first, bt_second;
13     TextView textView;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_first);
19 
20         initView();
21     }
22 
23     private void initView() {
24         // Associated controls
25         bt_first = (Button) findViewById(R.id.bt_first);
26         bt_second = (Button) findViewById(R.id.bt_Second);
27         textView = (TextView) findViewById(R.id.textview);
28         bt_first.setOnClickListener(new OnClickListener() {
29 
30             @Override
31             public void onClick(View arg0) {
32                 // The first jump,No data return jump
33                 Intent intent = new Intent(FirstActivity.this,
34                         SecondActivity.class);
35                 intent.putExtra("content", "The first way to jump");
36                 startActivity(intent);
37             }
38         });
39         bt_second.setOnClickListener(new OnClickListener() {
40 
41             @Override
42             public void onClick(View arg0) {
43                 // The second way,Jump with data return
44                 Intent intent = new Intent(FirstActivity.this,
45                         SecondActivity.class);
46                 intent.putExtra("content", "The second way to jump");
47 
48                 /*
49                  * The first parameter is an identifier of the request for the second parameter of the intent object
50                  */
51                 startActivityForResult(intent, 1);
52             }
53         });
54     }
55 
56     /*
57      * Through the startActivityForresult jump, the method to receive the returned data requestCode: the ID of the request
58      * resultCode:Identification data returned by the second page: data returned by the second page
59      */
60 
61     @Override
62     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
63         super.onActivityResult(requestCode, resultCode, data);
64         if (requestCode == 1) {
65             if (resultCode == 1) {
66                 String text = data.getStringExtra("content");
67                 textView.setText(text);
68             }
69         }
70     }
71 }

The code of SecondActivity is as follows:

 1 package com.example.intentdemo;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.TextView;
10 
11 public class SecondActivity extends Activity {
12     TextView textView;
13     Button button;
14     @Override
15     protected void onCreate(Bundle savedInstanceState) {
16         super.onCreate(savedInstanceState);
17         setContentView(R.layout.activity_second);
18         //Get the intent of the first page
19         Intent intent = getIntent();
20         
21         button = (Button) findViewById(R.id.button);
22         textView = (TextView) findViewById(R.id.textview);
23         //Receive data from the first page
24         textView.setText(intent.getStringExtra("content"));
25         button.setOnClickListener(new OnClickListener() {
26             
27             @Override
28             public void onClick(View arg0) {
29                 setResult(1, new Intent().putExtra("content", "Return from the second page"));
30                 //End current page
31                 finish(); 
32             }
33         });
34     }
35 }

 

 

 

This is a simple implementation,

If you don't understand it, you can post it below. I see it will help you solve it. Other Android problems can also be solved

Next, paste Demo:https://pan.baidu.com/s/16HEQ1pVnpB995i3-lAG7qw

Keywords: Android xml encoding

Added by uniflare on Fri, 06 Dec 2019 04:35:48 +0200