2.3 Activity between shuttle Intent
In the previous section we have learned how to create an Activity of. For an application, you certainly can not have only one Activity. Here's to learn more Activity is designed to jump.
2.3.1 using explicit Intent
For the creation of Activity of us are already familiar with, and quickly create a second Activity below. Named SecondActivity
. Well, the second Activity has been created, created the Activity Do not forget the need AndroidManifest.xml
to register. Since the default Android Studio has given us signed up, it does not, this is not the main Activity Activity will not need to configure <intent-filter>
up.
Here's how to start this second Activity, and this time we need Intent
this kind of.
Intent
Debut! Intent
It is an important way to interact with the various components of Android applications. It can indicate the desired operation is currently performed by component, may also pass data between different components. Intent can generally be used to start Activity, Service, send a broadcast. The latter two we have not yet learned, look at the start Activity.
Intent can be divided into: explicit and implicit Intent Intent . First look of the display using Intent.
Intent plurality constructor overloads, one of which is Intent(Context context,Class<?> cls)
. This method has two parameters, the first is context, context is to start Activity, and the second is the target you want to start the Activity Class. how to use? Activity class provides us with a method startActivity()
, passing Intent, you can start the Activity goal.
bt.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Toast.makeText(MainActivity.this,"显示内容",Toast.LENGTH_SHORT).show();
// 添加如下代码,启动 SecondActivity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
First introduced to MainActivity
this context, passing SecondActivity.class
as to start Activity. Such "intent" is very obvious. Completed SecondActivity start.
Use this way to start the "intent" of an Activity is very clear, and this is explicitly the Intent .