Android page Jump Transmission Parameters and page return receiving Parameters

Source: Internet
Author: User

Helloworldactivity. Java

package syit.david;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class HelloWorldActivity extends Activity{private Button btn;public final static int REQUEST_CODE = 1;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);btn = (Button) findViewById(R.id.btn1);btn.setOnClickListener(listener);}private OnClickListener listener = new OnClickListener(){public void onClick(View v){Intent intent = new Intent();intent.setClass(HelloWorldActivity.this, SecondActivity.class);intent.putExtra("str", "First Android Application!");//startActivity(intent);startActivityForResult(intent, REQUEST_CODE);}};@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data){if(requestCode==REQUEST_CODE){if(resultCode==SecondActivity.RESULT_CODE){Bundle bundle = data.getExtras();String str = bundle.getString("back");Toast.makeText(HelloWorldActivity.this, str, Toast.LENGTH_LONG).show();}}}}

Secondactivity. Java

package syit.david;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class SecondActivity extends Activity{private TextView tv;private Button btn;public final static int RESULT_CODE = 1;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.second);tv = (TextView)findViewById(R.id.secondText);Intent intent = getIntent();Bundle bundle = intent.getExtras();String s = bundle.getString("str");tv.setText(s);btn = (Button)findViewById(R.id.btn2);btn.setOnClickListener(listener);}public OnClickListener listener = new OnClickListener(){public void onClick(View v){Intent intent = new Intent();intent.putExtra("back", "Back Data");setResult(RESULT_CODE, intent);finish();}};}

The two XML files main and second in the layout directory

Main. xml

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

Second. xml

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

Androidmanifest. xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="syit.david"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="15" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".HelloWorldActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name=".SecondActivity"           android:label="@string/app_name">        </activity>    </application><uses-permission android:name="android.permission.CALL_PHONE"/><uses-permission android:name="android.permission.SEND_SMS"/></manifest>

Note:

1. After creating an android activity, you must register the declaration in the androidmanifest. xml file.

2. Use the startactivityforresult () method to PASS Parameters on the page, and write the parameters
Onactivityresult () method, which is used for processing.

3. Use the setresult () method on the receiving page to return the value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.