Intent -- open another Activity -- bidirectional value transfer

Keywords: Android xml Mobile encoding


In Android applications, we use the intent mechanism to jump between activities.

This example simply introduces how to use intent to make the program jump from MainActivity to another OtherActivity to realize a single parameter value. When returning MainActivity, use Bundle for batch return.

1, Design interface

1. MainActivity layout file

Open the RES / layout / activity main.xml file.
Enter the following code:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/open"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="Open another Activity(OtherActivity)" />  
  13.   
  14.     <TextView  
  15.         android:id="@+id/result"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="display OtherActivity Return results" />  
  19.   
  20. </LinearLayout>  

2. OtherActivity layout file

Open the res/layout/otheractivity.xml file.
Enter the following code:

  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/back"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="Return MainActivity" />  
  12.       
  13.     <TextView  
  14.         android:id="@+id/prompt"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="display MainActivity Pass value" />  
  18.   
  19. </LinearLayout>  


2, Procedure documents

1. Open the "Src / com. Genwoxue. Intent? B / mainactivity. Java" file.
Then enter the following code:

  1. package com.genwoxue.intent_b;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  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 MainActivity extends Activity {  
  12.   
  13.     private TextView tvResult=null;  
  14.     private Button btnOpen=null;  
  15.     @Override  
  16.      public void onCreate(Bundle savedInstanceState)     
  17.     {     
  18.         super.onCreate(savedInstanceState);                
  19.         setContentView(R.layout.activity_main);  
  20.           
  21.         tvResult=(TextView)super.findViewById(R.id.result);  
  22.           
  23.         btnOpen=(Button)super.findViewById(R.id.open);  
  24.           
  25.         //Call OtherActivity and pass value  
  26.         btnOpen.setOnClickListener(new OnClickListener(){  
  27.             public void onClick(View v)  
  28.             {    
  29.                 Intent intent=new Intent(MainActivity.this,OtherActivity.class);  
  30.                 intent.putExtra("prompt""Do you know Chiang's phone number and email address?");  
  31.                 MainActivity.this.startActivityForResult(intent, 1);  
  32.             }  
  33.         });  
  34.     }  
  35.       
  36.     //Processing received data  
  37.     @Override  
  38.     protected void onActivityResult(int requestCode,int resultCode,Intent data){  
  39.         switch(resultCode){  
  40.             case RESULT_OK:  
  41.                 super.onActivityResult(requestCode, resultCode, data);  
  42.                 //Receive data: use Bundle to transmit value  
  43.                 Bundle bundle =data.getExtras();    
  44.                 String employeeName=bundle.getString("employeeName");    
  45.                 String mobile=bundle.getString("mobile");    
  46.                 String email=bundle.getString("email");    
  47.                 tvResult.setText("☆☆☆☆☆☆Return results☆☆☆☆☆☆"+"\n Employees:"+employeeName+"\n phone number:"+mobile+"\n E-mail:"+email);  
  48.                 break;  
  49.             case RESULT_CANCELED:  
  50.                 tvResult.setText("Action cancel");  
  51.                 break;  
  52.             default:  
  53.                 break;  
  54.         }  
  55.     }  
  56. }             
  57.       


2. Open the "Src / com. Genwoxue. ContentProvider? B / otheractivity. Java" file.
Then enter the following code:

  1. package com.genwoxue.intent_b;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  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 OtherActivity extends Activity {  
  12.   
  13.     private TextView tvPrompt=null;  
  14.     private Button btnBack=null;  
  15.     @Override  
  16.      public void onCreate(Bundle savedInstanceState)     
  17.     {     
  18.         super.onCreate(savedInstanceState);                
  19.         setContentView(R.layout.activity_other);             
  20.         tvPrompt=(TextView)super.findViewById(R.id.prompt);  
  21.         btnBack=(Button)super.findViewById(R.id.back);  
  22.           
  23.         //receive data   
  24.         Intent intent=super.getIntent();  
  25.         String url=intent.getStringExtra("prompt");  
  26.         tvPrompt.setText(url);  
  27.           
  28.         //Return to MainActivity  
  29.         btnBack.setOnClickListener(new OnClickListener(){  
  30.             public void onClick(View v)  
  31.             {    
  32.                 Bundle bundle = new Bundle();    
  33.                 bundle.putString("employeeName""Jiang Laofu");    
  34.                 bundle.putString("mobile""139037100xx");    
  35.                 bundle.putString("email""hello@genwoxue.com");  
  36.                   
  37.                 Intent intent = new Intent(OtherActivity.this,MainActivity.class);    
  38.                 intent.putExtras(bundle);    
  39.                 OtherActivity.this.setResult(RESULT_OK, intent);    
  40.                 OtherActivity.this.finish();    
  41.             }  
  42.         });  
  43.     }  
  44. }             


3, Profile

Open the "Android manifest. XML" file.
Then enter the following code:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.intent_b"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="15" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.genwoxue.intent_b.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.           
  25.         <span style="color:#ff0000;"><strong><activity  
  26.             android:name="com.genwoxue.intent_b.OtherActivity">  
  27.         </activity>  
  28. </strong></span>    </application>  
  29.   
  30. </manifest>  

4, Operation results

    

Posted by jeny on Tue, 05 May 2020 04:46:03 -0700