In Android applications, we use the intent mechanism to jump between activities.
This example briefly introduces how to use intent to jump from MainActivity to another OtherActivity and realize one-way value transfer.
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" />
- </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/close"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Close current Activity(OtherActivity)" />
- <TextView
- android:id="@+id/orderno"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Show from MainActivity Pass value of!" />
- </LinearLayout>
2, Procedure documents
1. Open the file "Src / com. Genwoxue. ContentProvider [B / mainactivity. Java".
Then enter the following code:
- package com.genwoxue.intent_a;
- 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;
- public class MainActivity extends Activity {
- private Button btnOpen=null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btnOpen=(Button)super.findViewById(R.id.open);
- btnOpen.setOnClickListener(new OnClickListener(){
- public void onClick(View v)
- {
- Intent intent=new Intent(MainActivity.this,OtherActivity.class);
- intent.putExtra("info", "No66778899");
- MainActivity.this.startActivity(intent);
- }
- });
- }
- }
2. Open the file "src/com.genwoxue.contentprovider_b/OtherActivity.java".
Then enter the following code:
- package com.genwoxue.intent_a;
- 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 tvOrderNo=null;
- private Button btnClose=null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.otheractivity);
- tvOrderNo=(TextView)super.findViewById(R.id.orderno);
- btnClose=(Button)super.findViewById(R.id.close);
- Intent intent=super.getIntent();
- String orderNo=intent.getStringExtra("info");
- tvOrderNo.setText(orderNo);
- btnClose.setOnClickListener(new OnClickListener(){
- public void onClick(View v)
- {
- 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_a"
- 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_a.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>
- <strong><span style="color:#ff0000;"> <activity
- android:name="com.genwoxue.intent_a.OtherActivity">
- </activity>
- </span></strong> </application>
- </manifest>
4, Operation results