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:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/open"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Open another Activity(OtherActivity)" />
- <TextView
- android:id="@+id/result"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="display OtherActivity Return results" />
- </LinearLayout>
2. OtherActivity layout file
Open the res/layout/otheractivity.xml file.
Enter the following code:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/back"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Return MainActivity" />
- <TextView
- android:id="@+id/prompt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="display MainActivity Pass value" />
- </LinearLayout>
2, Procedure documents
1. Open the "Src / com. Genwoxue. Intent? B / mainactivity. Java" file.
Then enter the following code:
- package com.genwoxue.intent_b;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView tvResult=null;
- private Button btnOpen=null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tvResult=(TextView)super.findViewById(R.id.result);
- btnOpen=(Button)super.findViewById(R.id.open);
- //Call OtherActivity and pass value
- btnOpen.setOnClickListener(new OnClickListener(){
- public void onClick(View v)
- {
- Intent intent=new Intent(MainActivity.this,OtherActivity.class);
- intent.putExtra("prompt", "Do you know Chiang's phone number and email address?");
- MainActivity.this.startActivityForResult(intent, 1);
- }
- });
- }
- //Processing received data
- @Override
- protected void onActivityResult(int requestCode,int resultCode,Intent data){
- switch(resultCode){
- case RESULT_OK:
- super.onActivityResult(requestCode, resultCode, data);
- //Receive data: use Bundle to transmit value
- Bundle bundle =data.getExtras();
- String employeeName=bundle.getString("employeeName");
- String mobile=bundle.getString("mobile");
- String email=bundle.getString("email");
- tvResult.setText("☆☆☆☆☆☆Return results☆☆☆☆☆☆"+"\n Employees:"+employeeName+"\n phone number:"+mobile+"\n E-mail:"+email);
- break;
- case RESULT_CANCELED:
- tvResult.setText("Action cancel");
- break;
- default:
- break;
- }
- }
- }
2. Open the "Src / com. Genwoxue. ContentProvider? B / otheractivity. Java" file.
Then enter the following code:
- package com.genwoxue.intent_b;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class OtherActivity extends Activity {
- private TextView tvPrompt=null;
- private Button btnBack=null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_other);
- tvPrompt=(TextView)super.findViewById(R.id.prompt);
- btnBack=(Button)super.findViewById(R.id.back);
- //receive data
- Intent intent=super.getIntent();
- String url=intent.getStringExtra("prompt");
- tvPrompt.setText(url);
- //Return to MainActivity
- btnBack.setOnClickListener(new OnClickListener(){
- public void onClick(View v)
- {
- Bundle bundle = new Bundle();
- bundle.putString("employeeName", "Jiang Laofu");
- bundle.putString("mobile", "139037100xx");
- bundle.putString("email", "hello@genwoxue.com");
- Intent intent = new Intent(OtherActivity.this,MainActivity.class);
- intent.putExtras(bundle);
- OtherActivity.this.setResult(RESULT_OK, intent);
- OtherActivity.this.finish();
- }
- });
- }
- }
3, Profile
Open the "Android manifest. XML" file.
Then enter the following code:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.genwoxue.intent_b"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="15" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.genwoxue.intent_b.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>
- <span style="color:#ff0000;"><strong><activity
- android:name="com.genwoxue.intent_b.OtherActivity">
- </activity>
- </strong></span> </application>
- </manifest>
4, Operation results