..
In the previous lesson, introducing Intent, we introduced a powerful tool to connect more activity within an application. This will make it possible to build applications, even of considerable complexity, delegating specific activity to some activity, and these operations will be activated only if necessary as a result of user interactions.
In this lesson we will see how to invoke a second from the main activity.
So, let's create a new project and the first thing we're going to create a new Java class SecondaAttivita.java kimono. Within this file, insert the following code:
MrWebMaster.android package;
android.app.Activity imports;
android.os.Bundle imports;
android.widget.TextView imports;
android.view.LinearLayout imports;
extends Activity {public class SecondaAttivita
private TextView tv;
@ Override
public void onCreate (Bundle savedInstanceState) {
LinearLayout linear LinearLayout = new (this);
tv.setText ("This is the second activity");
linear.addView (TV);
setContentView (linear);
}
}
As we can see this second class, being an activity, extend precisely the Activity.
We are now going to edit the code for the Activity class HelloWorld.java main positioning ourselves, of course, in the method onCreate. Then insert these two simple lines of code:
MiaIntent Intent intent = new (this, SecondaAttivita.class); startActivity (miaIntent);
In the first the first line we are going to instantiate an object of type Intent and the type of intent that will be created will un'intent explicit. Fact is used in which a manufacturer must specify the activity that launches the intent (in our case the activity in which we find ourselves and therefore we pass this as a value) and the activity that is directed Intent.
The method startActivity (miaIntent) is the method that takes care of launching a new activity. The latter is triggered by the intent that we created in the previous line.
At this point if we tried to launch our application will however continue to see the string of Activity main greeting instead of printing the second criminal activity. This is because in addition to structure the call via the Activity Code must also declare it in the secondary AndroidManifest.xml file.
So, let's open the file and after the declaration of Activity AndroidManifest.xml primary insert, listing in XML, the following lines of code:
<activity android:name=".SecondaAttivita"> </ Activity>
In this way we have said of Activity in the presence of 'AndroidManifest and if now launched our application will get the desired result.
AndroidManifest within the file, in the declaration of new activity, you can set the desired intent filter as needed. This makes it possible to model the behavior of each activity was given the opportunity to respond to the intent with the same features included in order filter.
In the above example we have presented a second primary in a very structured activity. And 'course, you can create a new xml file to structure a more complex layout of the window and then pass as an argument to the method of the second activity setContentView just that file.
| |
CSS (Course)
Web Design and Accessibility according to W3C CSS and XHTML. Starting from 29 €. |
| |
E-commerce with ASP (Ebook)
ECommerce and Shopping Cart with ASP. Only 35 €. |
| |
HTML (Course)
The markup language for the Web from 29 €. |