Toast
Toast is a very good reminder provided by Android system. It can be used in programs to notify users of some short information, which will disappear automatically after a period of time and will not occupy any screen space.
Call code: Toast.makeText(FirstActivity.this, "You clicked Button 1", Toast.LENGTH_SHORT).show();
Menu Creation Method
Create a new menu folder under res directory, create a new main menu file under the folder, and then add the following code in main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add_item"
android:title="Add"/>
<item android:id="@+id/remove_item"
android:title="Remove"/>
</menu>
Then go back to FirstActivity to rewrite the onCreateOptionsMenu() method (rewrite method shortcut key: Ctrl+O)
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
Menu Response Event: Override the onOptions ItemSelected () method in FirstActivity:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_item:
Toast.makeText(this,"You clicked Add",Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this,"You clicked Remove",Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
Method of Destruction of Activities
finish();
Intent
Definition:
Intent is an important way of interaction between components in Android programs. It can not only specify the actions that the current components want to perform, but also transfer data between different components. Intent can generally be used to start activities, start services, and send broadcasts and other scenarios.
Classification:
Explicit Intent
#### Launch target activities:
"`
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
### Implicit Intent #### Implicit Intent does not specify which activity to start, but specifies a series of more abstract information such as action and category, and then leaves it to the system to analyze the Intent and help us find the appropriate activity to start.
<category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
"`
This activity can only respond to the intent if the content in the middle can match the action and category specified in Intent at the same time.
The click event code of the button in FirstActivity is as follows:
button1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent=new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);
}
}
Implicit Intent displays a web page:
public void onClick(View v){
Intent intent =new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
Implicit Intent Call System Dial-up Interface
public void onClick(View v){
Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
intent passes data to the next activity
FirstActivity Sends:
public void onClick(View v){
String data="Hello SecondActivity";
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("extra_data",data); startActivityForResult(intent);
}
Second Activity takes out:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Intent intent=getIntent();
String data=intent.getStringExtra("extra_data");
Log.d("SecondActivity", data);
}
Return data to the previous activity
FirstActivity:
public void onClick(View v){
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent,1);
}
SecondActivity:
public void onClick(View view) {
Intent intent =new Intent();
intent.putExtra("data_return","Hello FirstActivity");
setResult(RESULT_OK,intent);
finish();
}
FirstActivity rewrites the onActivityResult method to get the return data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case 1:
if (resultCode==RESULT_OK){
String returnedData=data.getStringExtra("data_return");
Log.d("FirstActivity", returnedData);
}
break;
default:
}
}