Realize page Jump - Intent

Keywords: Android xml

Intent can be understood as a messenger (intention), which cooperates to complete the communication between Android components.

Intent to realize page Jump

1. Jump from page A to page B directly: startActivity(intent)

2. Start page B directly, and cut page B to return data to A: startActivityForResult(intent,requestCode),

Use in page A: onactiovityresult (int requestcode, int resultcode, int data)

Use: setResult(result Code, data) in page B;

The second way is more flexible.

Here is the code for two pages

Page A first

XML code:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:id="@+id/button1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:id="@+id/button2"
        android:layout_marginTop="120dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Here is the returned data"
        android:id="@+id/textView"
        android:layout_marginTop="180dp" />
</RelativeLayout>

Business logic code:

package com.xuzone.appbasic.intent;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class FIntent extends Activity {

    private Button btn1;
    private Button btn2;
    private TextView tv;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);
        //Through btn1 to realize page Jump,
        //1, How to startActivity
        //  First, initialize Intetn to establish intention, and then start intention through startActivity

        mContext=this;
        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);
        tv=(TextView)findViewById(R.id.textView);

        //Register listening events
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //The first parameter is the context object this, and the second parameter is the target file
                Intent intent=new Intent(mContext, SIntent.class);
                startActivity(intent);
            }
        });

        //Through btn2 to realize page Jump,
        //2, Through startActivityForResult mode
        //Register listening events
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,SIntent.class);

                //The first object is the intent parameter, and the second is an identifier of the request
                startActivityForResult(intent, 1);

            }
        });
    }
    //Jump through startActivityForResult to accept the method of returning data
    //requestCode indicates the ID of the request
    //Identifier returned by the second page of resultCode
    //data returned from the second page
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode==1&&resultCode==2){
            String content=data.getStringExtra("data");
            tv.setText(content);
        }

    }
}

Page B

XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.xuzone.appbasic.intent.SIntent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="72dp"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

Business logic code

package com.xuzone.appbasic.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SIntent extends Activity {

    private Button Btn;
    private String content="Hello, I'm from B Data returned from the page.";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent2);
        //The second page sends back data to the first page
        //The bit intent object actually returned by the returned data
        Btn = (Button)findViewById(R.id.button);
        Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent data=new Intent();
                data.putExtra("data",content);
                setResult(2,data);
                //Destroy end current page
                finish();
            }
        });
    }
}

 

 

Posted by thetechgeek on Thu, 19 Dec 2019 11:16:08 -0800