Intent -- open another Activity -- single pass value

In Android applications, we use the intent mechanism to jump between activities. This example briefly introduces how to...


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:

  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. <Button
  8. android:id="@+id/open"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="Open another Activity" />
  12. </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. <Button
  7. android:id="@+id/close"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="Close current Activity(OtherActivity)" />
  11. <TextView
  12. android:id="@+id/orderno"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Show from MainActivity Pass value of!" />
  16. </LinearLayout>


2, Procedure documents

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

  1. package com.genwoxue.intent_a;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends Activity {
  9. private Button btnOpen=null;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState)
  12. {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. btnOpen=(Button)super.findViewById(R.id.open);
  16. btnOpen.setOnClickListener(new OnClickListener(){
  17. public void onClick(View v)
  18. {
  19. Intent intent=new Intent(MainActivity.this,OtherActivity.class);
  20. intent.putExtra("info", "No66778899");
  21. MainActivity.this.startActivity(intent);
  22. }
  23. });
  24. }
  25. }

2. Open the file "src/com.genwoxue.contentprovider_b/OtherActivity.java".
Then enter the following code:

  1. package com.genwoxue.intent_a;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. public class OtherActivity extends Activity {
  10. private TextView tvOrderNo=null;
  11. private Button btnClose=null;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState)
  14. {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.otheractivity);
  17. tvOrderNo=(TextView)super.findViewById(R.id.orderno);
  18. btnClose=(Button)super.findViewById(R.id.close);
  19. Intent intent=super.getIntent();
  20. String orderNo=intent.getStringExtra("info");
  21. tvOrderNo.setText(orderNo);
  22. btnClose.setOnClickListener(new OnClickListener(){
  23. public void onClick(View v)
  24. {
  25. finish();
  26. }
  27. });
  28. }
  29. }

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_a"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="15" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.genwoxue.intent_a.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <strong><span style="color:#ff0000;"> <activity
  23. android:name="com.genwoxue.intent_a.OtherActivity">
  24. </activity>
  25. </span></strong> </application>
  26. </manifest>


4, Operation results

4 May 2020, 17:21 | Views: 5030

Add new comment

For adding a comment, please log in
or create account

0 comments