Android multiple Activity passes

  • 2020-05-17 06:30:36
  • OfStack

Here is the code for main Activity:

Development: pass-through between Activity - 51CTO.COM


package com.chaoyang.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.style.BulletSpan;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button =(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

   // Register the button with a click event to open a new one Acticity
         @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
          // for Intent Sets the component to be activated (to be activated) TheOtherActivity this Activity ) 
    Intent intent =new Intent(MainActivity.this,TheOtherActivity.class);//
    // writing 1 intent.setClass(MainActivity.this, OtherActivity.class);// Sets the component to be activated 
    // writing 2 intent.setComponent(new ComponentName(MainActivity.this, TheOtherActivity.class));// Sets the component to be activated 

    // The first 1 Seed transfer method ( The code looks much cleaner )
    /*
    intent.putExtra("name", "dinglang");
      intent.putExtra("age", 22);
      */
    // The first 2 Seed transfer method 
    Bundle bundle =new Bundle();
    bundle.putString("name", "dinglang");
    bundle.putInt("age", 22);
    intent.putExtras(bundle);
    /*
     Intent Provides a variety of common types after overloading putExtra() Methods, such as:  putExtra(String name, String value) ,  putExtra(String name, long value) In the putExtra() The method internally determines the current Intent Whether the object already exists inside 1 a Bundle Object, if it doesn't exist it will be created Bundle Object, called later putExtra() The value passed in from the method is stored there Bundle object 
                                             These can actually look at the source, the internal implementation of the principle is 1 The sample of 
     */
    //startActivity(intent);// This can be activated without receiving the component's return value 
    // The return result needs to be received. Note the result code returned 
    startActivityForResult(intent, 100);
         }
  });
    }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub

  Toast.makeText(this, data.getStringExtra("result"), 1).show();// Get back the result 
  super.onActivityResult(requestCode, resultCode, data);
 }
}

Here is part of the otherActivity code:

Under the same package, create a new class, inherit the Activity class, override the onCreate method...


package com.chaoyang.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TheOtherActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.other);// Set the Activity The corresponding xml Layout file 
  Intent intent =this.getIntent();// Get her intentions activated 
  String name =intent.getStringExtra("name");
  int age=intent.getExtras().getInt("age");// The first 2 Species value method 
  TextView textView = (TextView)this.findViewById(R.id.result);
  textView.setText(" Name: "+ name+"   Age: "+ age);
  Button button = (Button)this.findViewById(R.id.close);
  button.setOnClickListener(new View.OnClickListener() {

   // Returns the result to the previous Activity
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent =new Intent();
    intent.putExtra("result", " This is the result ");
    setResult(20, intent);// Set the return data 
    finish();// Shut down activity
   }
  });
 }
}

Create a new layout file of XML in the layout folder between Activity and Activity. (if you choose to create Activity when creating the Android project, a new XML layout file will be created by default.)

Here is the layout file main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

    <Button  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" Open the OtherActivity"
     android:id="@+id/button"
     />
</LinearLayout>

Below is the layout file other.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=" This is a OtherActivity"
    android:id="@+id/result"
    />

      <Button  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=" Shut down Activity"
    android:id="@+id/close"
    />
</LinearLayout>

Finally, notice the modification of the project manifest file. Add in and register the new Acticity name

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.chaoyang.activity"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--  Note the addition to the project manifest file  -->
<activity android:name="TheOtherActivity" android:label="the other Activity"/>
    </application>
</manifest>

Points to note:

When using the Intent component for attachment data, write both ways of passing values between Activity.

It is worth mentioning the role of the Bundle class
The Bundle class is used to carry data, which is similar to Map and is used to hold values in the form of key-value name-value pairs. In contrast to Map, it provides various common types of putXxx()/getXxx() methods, such as putString()/getString() and putInt()/getInt(), putXxx() for putting data into Bundle objects, and getXxx() for getting data from Bundle objects. Bundle actually USES HashMap internally < String, Object > A variable of type to hold the values put in by the putXxx() method.

Also, in the onActivityResult method, the first parameter is the request code, which is the past value passed by the call startActivityForResult(), and the second parameter is the result code, which is used to identify which new Activity the returned data comes from. They all play the role of simple identification (don't get confused with status codes such as 404,200 in http protocol). They can be filled in and matched according to their own business requirements. If necessary, they can be judged according to this.

I won't go into the details here.


Related articles: