Android study notes Activity USES Intent to pass value sample code

  • 2020-05-10 18:50:43
  • OfStack

Intent, also known as intents, is a runtime binding mechanism that links two different components (Activity, Service, BroadcastReceiver) as the program runs. With Intent, a program can express a request or wish to Android, and Android will select the appropriate component to request based on the content of the wish.

In the communication between these components, Intent mainly assists. Intent is responsible for describing the action of one operation in the application, the data involved in the action and the additional data; Android is responsible for finding the corresponding component according to the description of Intent, passing Intent to the calling component, and completing the invocation of the component. Thus, Intent ACTS as a media mediator here, providing information about the components that call each other and decoupling the caller from the called.
To request Activity via Intent, the requested Activity new label configuration must be configured in the AndroidManifest.xml file, otherwise it will cause an error.

Intent1 generally contains two main messages, action and data.
action: represents the action of this Intent operation.
data: represents the data involved in this action.

An example shows that Intent is used in Activity to guide the new Activity and pass the data. This program only jumps between two pages, but each jump will create a new Activity, so after startActivity(), it needs to call finish() to destroy the current Activity. If not, after several jumps, multiple Activity will be stored in the Activity stack of the program. Click the back button of the device, you will find that 1 will go straight back.

Main steps:
New Android project, add new layout file other.xml, add Activity class otherActivity.class to accept Intent and display other.xml.
In the MainActivity class, declare an Intent class and specify the source and destination through the constructor of Intent.
Once you have the Intent, pass in the data using the Intent.putExtra () method.
Call Activity.startActivity to start the Intent.
In the otherActivity class, use Activity.getIntent () to get Intent for the current Activity.
Once you have the Intent, use the Intent.getXxxExtra () method to get the data saved in it.
Configure the otherActivity node at AndroidManifest.xml.

The sample code
Step 2-3:
 
public class MainActivity extends Activity { 
private TextView textView; 
private Button btn; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
textView=(TextView)findViewById(R.id.textView1); 
btn=(Button)findViewById(R.id.button1); 
btn.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
// Intent Constructor: Intent Source; Intent Purpose.  
Intent intent =new Intent(MainActivity.this,otherActivity.class); 
intent.putExtra("data", " It's currently a page 2 , the information comes from the page 1"); 
startActivity(intent);// Start the Activity 
finish(); 
} 
}); 
} 
} 

Step 4-5:
 
public class otherActivity extends Activity { 
private Button btn; 
private TextView textView; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
setContentView(R.layout.other); 
textView=(TextView)findViewById(R.id.textView2); 
btn=(Button)findViewById(R.id.button2); 
// through Activity.getIntent() Gets what the current page received Intent .  
Intent intent =getIntent(); 
//getXxxExtra Methods to obtain Intent The data that was passed  
String msg=intent.getStringExtra("data"); 
textView.setText(msg); 
btn.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
Intent intent=new Intent(otherActivity.this,MainActivity.class); 
startActivity(intent); 
finish(); 
} 
}); 
} 
} 

Step 7:
 
<application 
android:allowBackup="true" 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" 
android:theme="@style/AppTheme" > 
<activity 
android:name="cn.bgxt.IntentForAc.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> 
<activity android:name=".otherActivity"/> 
</application> 

Return data from Activity
In the above example, we only introduced that Activity transfers data through Intent. However, in practical application, we not only need to transfer data to Activity, but also need to return data from Activity. Although returning data is similar to transferring data, there are some differences.
The main differences are as follows:
Passing the data requires Activity to be started using the Activity.startActivityForResult () method, which requires passing the request code instead of Activity.startActivity ().
When returning data, call the Activity.setResult () method to set the return Intent and the return code.
The onActivityResult() method of the source Activity needs to be overridden to accept the returned Intent, where the request code and response code are judged.
Return data from Activity with an example. This program has two Activity, input the calculation number of addition operation in MainActivity, jump to otherActivity, input the calculation result, and after clicking back, output the calculation result to MainActivity.
The sample code
MainActivity:
 
public class MainActivity extends Activity { 
private EditText one,two,result; 
private Button btn; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
one=(EditText)findViewById(R.id.one); 
two=(EditText)findViewById(R.id.two); 
result=(EditText)findViewById(R.id.result); 
btn=(Button)findViewById(R.id.btnGo); 
btn.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
int ione=Integer.parseInt(one.getText().toString()); 
int itwo=Integer.parseInt(two.getText().toString()); 
Intent intent=new Intent(MainActivity.this, otherActivity.class); 
intent.putExtra("one", ione); 
intent.putExtra("two", itwo); 
// Start the one that needs to listen for the return value Activity , and set the request code: requestCode 
startActivityForResult(intent, 1); 
} 
}); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 
// when otherActivity This method is responded to when data is returned from  
//requestCode and resultCode Must and request startActivityForResult() And return setResult() The value passed in when 1 Cause.  
if(requestCode==1&&resultCode==2) 
{ 
int three=data.getIntExtra("three", 0); 
result.setText(String.valueOf(three)); 
} 
} 
@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; 
} 
} 

otherActivity:
 
public class otherActivity extends Activity { 
TextView tvShow; 
EditText etResult; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.other); 
tvShow=(TextView)findViewById(R.id.tvShow); 
etResult=(EditText)findViewById(R.id.etResult); 
Intent intent=getIntent(); 
int a=intent.getIntExtra("one", 0); 
int b=intent.getIntExtra("two", 0); 
tvShow.setText(a+" + "+b+" = "+" ? "); 
Button btnResult=(Button)findViewById(R.id.btnReturn); 
btnResult.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
// New statement 1 a Intent Used to store the data put back  
Intent i=new Intent(); 
int result=Integer.parseInt(etResult.getText().toString()); 
i.putExtra("three", result); 
setResult(2, i);// Set up the resultCode . onActivityResult() Can be obtained  
finish();// Ends the current session after use Activity Life cycle  
} 
}); 
} 
} 

Related articles: