SoFunction
Updated on 2025-04-09

Android uses Intent to achieve page jump

What is Intent

Intent can be understood as messenger (intention)
Intent assists in communication between various Android components

Intent implements jump between pages

1>startActivity(intent)
2>startActivityForResult(intent,requestCode)
onActivityResult(int requestCode,int resultCode,Intent data)
setResult(resultCode,data)
The second startup method can return results

Code


public class FirstActivity extends AppCompatActivity {
  private Button bt1;
  private Button bt2;
  private TextView tv;
  private Context context=;

  protected void onCreate(@Nullable Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.first_layout);

    bt1 = (Button) findViewById(.button_first);
    bt2 = (Button) findViewById(.button_second);
    tv = (TextView) findViewById();
    /*
     * Click bt1 to achieve jump between pages
     * to achieve
     * */
    (new () {
      public void onClick(View view) {
        /*
         * First parameter: context object. This anonymous inner class cannot be directly.this
         * The second parameter: target file (note that it is .class)
         * Intent(Context packageContext, Class<?> cls)
         * */
        //Intent intent = new Intent(,);
        Intent intent = new Intent(context,);
        startActivity(intent);
      }
    });

    /*
     * By startActivityForResult
     * */
    (new () {
      public void onClick(View view) {
        Intent intent = new Intent(context,);
        /*
          * The first parameter is the Intent object
          * The second parameter is a requested identifier
          * public void startActivityForResult(Intent intent, int requestCode)
          */
        startActivityForResult(intent,1);
      }
    });
  }
  /*
    * The method to receive the return data by jumping through the startActivityForResult method
    * requestCode: the requested identifier
    * resultCode: The flag returned by the second page
    * data: data returned by the second page
    */
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    (requestCode, resultCode, data);
    if(requestCode == 1 &amp;&amp; resultCode == 3){
      String value = ("data");
      (value);
    }
  }
}


public class SecondActivity extends Activity {
  private Button bt1;
  private String value="Return data";

  protected void onCreate(@Nullable Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.second_layout);

    bt1 = (Button) findViewById();
    /*
     * When will the second page pass data back to the first page?
     * The first page is actually an Intent object
     * */

    (new () {
      public void onClick(View view) {
        Intent data = new Intent();//Just use empty parameters, because we don't jump        ("data",value);
        setResult(3,data);
        finish();//Destroy this page, that is, return to the previous page      }
    });
  }
}

first_layout.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:andro
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;

  &lt;Button
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="The first method of starting" /&gt;

  &lt;Button
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="The second way to start" /&gt;

  &lt;TextView
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show the data returned from the second page" /&gt;
&lt;/LinearLayout&gt;

second_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">

 <Button
  android:
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Button" />
</LinearLayout>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.