Android: Use Intent to return the result, androidintent
This chapter uses Intent to return the expected results;
Process:
Start the home page-> press the button to start the second activity-> enter the user name, press OK-> return the user name back to the home page-> Read and display the user name on the home page.
1. Start the Home Page:
2. Start the second activity.
3. Enter the User Name:
4. Go back to the home page and read and display:
Create a project and enter the corresponding code.
1 homepage code (activity_main.xml ):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.intenttest.MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/activate_secondary" android:onClick="onClick" /></LinearLayout>
Create a Button and specify the corresponding time as onClick
2 response time code of the Home Page:
public class MainActivity extends Activity {/* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the * startActivityForResult() method is equivalent to calling it using * the startActivity() method. That is, no result will be returned. */int requestCode = 1;public static<T> boolean equ(T t1, T t2){return t1 == t2;} @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view){ startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode); }}
StartActivityForResult is not used for startActivity. You can use this function to return the result.
3. Enter the code of the second activity:
<?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="horizontal" > <EditText android:id="@+id/txt_username" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="@string/your_name" /> <Button android:id="@+id/btn_OK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="@string/ok_button" /></LinearLayout>
A simple input text and a button
4. Logic code of the Second activity
package com.example.intenttest;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class SecondaryActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.secondary_activity);}public void onClick(View v){Intent data = new Intent();EditText txt_username = (EditText) findViewById(R.id.txt_username);data.setData(Uri.parse(txt_username.getText().toString()));setResult(RESULT_OK, data);finish();}}
It is mainly the onClick code. After creating an Intent data, set the Intent content and use setResult to return the Intent to the homepage activity. In fact, the basic principle is to use Intent to communicate.
5 code for reading and displaying the homepage logic:
@Override public void onActivityResult(int reqCode, int resCode, Intent data) { if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){ Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show(); } }The processing function is automatically called when the previous corresponding activity returns. The expression symbol is that requestCode corresponds to the returned identifier reqCode, so that the returned Intent is returned by the corresponding activity.
The complete homepage logic code is as follows:
package com.example.intenttest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {/* * An integer value that identifies an activity you are calling. * When an Activity returns a value, you must have a way to identify it * If the request code is set to -1, then calling it using the * startActivityForResult() method is equivalent to calling it using * the startActivity() method. That is, no result will be returned. */int requestCode = 1;public static<T> boolean equ(T t1, T t2){return t1 == t2;} @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view){ startActivityForResult(new Intent("su.bill.intenttest.SecondaryActivity"), requestCode); } @Override public void onActivityResult(int reqCode, int resCode, Intent data) { if (equ(reqCode, requestCode) && equ(resCode, RESULT_OK)){ Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
Other codes that are not mentioned are automatically generated when a project is created. You can ignore them temporarily.
It is just familiar with some Android mechanisms, but it is not difficult to understand.
It seems that the-day program cannot be completed within a hundred days, because there are many other things to be done.
Try to do it quickly. In the future, we will offer some excellent tutorials. It makes sense to let more people learn what they have learned.