Android studio implements information transfer between two windows: intent

intent introduction

  1. So how on earth do we jump from one window to another, and how does information from one window pass to another? Yes, through intent. Let's briefly introduce intent.
  2. There are two ways to use Intent in Android: explicit Intent and implicit Intent.
//Explicit intent (this code block only describes how one window jumps to another)
//Method 1:
//Create an Intent object and specify the class name to start. That is, if the intent object is started, the window will correspond to MainActivity
//Window jumps to the window corresponding to Second Activity

SecondActivity Intent intent=new Intent(MainActivity.this, SecondActivity.class); 

//Start intent

startActivity(intent);

//Method 2: In addition to jumping the window by specifying the class name, explicit Intent can also refer to the package name and full path of the target component.
//A window that must jump.
//setClassName("package name", "full path name of class");
intent.setClassName("com.jxust.cn","com.jxust.cn.chapter_shengtime");
//Start Activity
startActivity(intent);


//Implicit intent (I don't understand, but I put other people's notes below):
//Without specifying Activeness to start in the program, the Android system will be set up in the Android manifest. XML file.
//Actions, categories, data (Uri and data types) to start the appropriate components.

<activity android:name=".MainActivity">
  <intent-filter>
<!—Set up action Attributes, according to name Set the value to specify the component to start-->
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

//Description: The < action > tag specifies the action that the current Activity can respond to as android.intent.action.MAIN, and
//The < category > tag contains some category information, and Activity will start only if the content of both matches at the same time.
ntent intent=new Intent();
Intent.setAction("android.intent.action.MAIN");
StartActivity(intent);
//The jump between windows is realized. How can information be transferred between windows? Now let's introduce it.
Intent intent=new Intent(this,SecondActivity.class);
//Transfer parameters
intent.putExtra(key, value);//"Value" can be any type of data, "key" is to give "value" a name called "key".
                       //Keys can be used for indexing
startActivity(intent);



//If you need to pass more parameters, you need to use putExtras() method to pass data. This method passes the Bundle object. The specific code is as follows:
Intent intent=new Intent(this,SecondActivity.class);
Bundle bundle=new Bundle();
bundle.putString("phone","123456");
bundle.putString("sex","male");
bundle.putString("age","18"); 
intent.putExtras(bundle);
startActivity(intent); 




//How does the booted window receive data?
Intent intent=this.getIntent();
String receive_str=intent.getStringExtra(key);//” The key is used here.
Use onActivityResult to get the return value of the jumped target window:


If you start an Activity and want to return the result to the current Activity, you can start the Activity using the startActivityForResult() method


startActivityForResult(Intent intent, int requestCode)
The first parameter is normal Intent, specifying the NewActivity to start
 The second parameter is the request code, which calls startActivityForResult() to pass past values.


In order to get the return result of the activated Activity, the following two steps need to be performed:
1. Activation that is started needs to call the setResult(int resultCode,Intent data) method to set the returned result data
 2. Activity before jumping rewrites onActivityResult (int request Code, int resultCode, Intent intent) method to receive result data


onActivityResult(int requestCode, int resultCode, Intent data) 
The first parameter is the request code, which calls startActivityForResult() to pass past values.
The second parameter is the result code, which identifies the new Activity from which the returned data comes.
The third parameter is the data returned from NewActivity
What is the difference between using onActivityResult to get the return value and intent.getStringExtra to get the return value?
Unfilled pit.

Let's have a real fight...

Objectives:

  1. Make a login and registration interface: click the registration button in the login interface, and then jump to the registration interface. After registering, click on it and it will jump to the login page. At this time, the login page has automatically filled in the registered information. Click on login and jump to an interface that displays "Welcome Xiaoming"

  2. We're going to create three windows, so we're going to create three activities, three layouts. The three activities are MainActivity (login) Activity 2 (click on the login to jump to the welcome interface) Activity 3 (registration). The three activities correspond to three layouts: activity_main, mylayout2, mylayout.

 

1. In MainActivity, fill in:

package com.example.lesson3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    //Create two Button s for login and registration, and two EditText s for entering accounts and passwords
    private Button btnLogin,btnReg;
    private EditText edtName,edtPwd;
    private final int REQUEST_CODE=101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        btnReg = (Button) findViewById(R.id.btnReg);
        edtName = (EditText) findViewById(R.id.edtName);
        edtPwd = (EditText) findViewById(R.id.edtPwd);

        //Click on login to pass the username to Activity 2
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, Activity2.class);
                String name=edtName.getText().toString();
                String pwd=edtPwd.getText().toString();
                intent.putExtra("name",name);
                intent.putExtra("pwd",pwd);
                startActivity(intent);
            }
        });

        //Click Register and jump to Activity 3. Registration,
        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this, Activity3.class);
                startActivityForResult(intent,REQUEST_CODE);
            }
        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUEST_CODE){
            String name=data.getStringExtra("name");
            String pwd=data.getStringExtra("pwd");
            edtName.setText(name);
            edtPwd.setText(pwd);
        }
    }

}

2. Fill in Activity 3:

package com.example.lesson3;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Activity3 extends Activity {
    private Button btnReg;
    private EditText edtName, edtPwd, edtRePwd;
    private static final int RESULT_CODE = 101;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
        btnReg = (Button) findViewById(R.id.btnReg);
        edtName = (EditText) findViewById(R.id.edtName);
        edtPwd = (EditText) findViewById(R.id.edtPwd);
        edtRePwd = (EditText) findViewById(R.id.edtRepwd);

        btnReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edtName.getText().toString();
                String pwd = edtPwd.getText().toString();
                String repwd = edtRePwd.getText().toString();
                if (!"".equals(pwd) && pwd.equals(repwd)) {
                    //Get the Intent object that starts the Activity
                    Intent intent = getIntent();
                    intent.putExtra("name", name);
                    intent.putExtra("pwd", pwd);
                    //Set the result code and set the Activity returned after completion
                    setResult(RESULT_CODE, intent);
                    //End RegActivity
                    Activity3.this.finish();
                } else {
                    Toast.makeText(Activity3.this, "Inconsistent password input", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

3. Fill in Activity 2:

package com.example.lesson3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Activity2 extends Activity {
    private TextView welcome;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout2);
        welcome=(TextView)findViewById(R.id.welcome);
        Intent intent=this.getIntent();
        String name=intent.getStringExtra("name");
        welcome.setText("Hello "+name);
    }
}

4. In Android Manifest. XML

4,stay AndroidManifest.xml in
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lesson3">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

//The following two activities are newly added:
       <activity android:name=".Activity3">
        </activity>
        <activity android:name=".Activity2">
        </activity>
    </application>

</manifest>

5. Drag and drag in layout to form an interface

 

 

Keywords: Android xml Windows encoding

Added by godwheel on Mon, 23 Sep 2019 15:41:06 +0300