Lexioni - 09.programim Mobile
Lexioni - 09.programim Mobile
Cikël leksionesh
në "Programim
Mobile"
Ky kurs mëson studentët se si për të ndërtuar Apps celular për Android, iOS dhe
Windows 8, trininë që është platforma operative celulare e sotme.Android eshte
nje software stack per mobile devices qe perfshihen ne nje sistem operativ,
middleware dhe aplikacionet celes.Android SDK siguron tools dhe API-te e
nevojshme per te filluar zhvillimin e aplikacionit ne platformen Android. Ky Leksioni IX
kurs ofron një hyrje në zhvillimin e softuerit celular për ata me përvojë ne
programimim te orjentuar ne objekte ne Java si dhe njohuri ne formatin XML.
Intent
In the previous lesson we have created an application that consists of two Activities. I will
remind you that to create an Activity you need to:
- create a class that extends android.app.Activity
- create an Activity-tag in the manifest-file and specify the class created for it in the Name
field.
I hope the previous lesson wasn’t too complicated and you are now comfortable with Activity
creation procedure. Now we can pay attention to Activity invocation code.
1 Intent intent = new Intent(this, ActivityTwo.class);
2 startActivity(intent);
We’ve used an Intent object. You can read about it here, here and here. But the information is
quite complicated for understanding from scratch. I will try to explain in my own words.
What Intent is
In our case Intent is an object in which we specify, which Activity we need to invoke. After it
we pass this Intent-object to startActivity method, which finds the corresponding Activity and
shows it. When creating an Intent we have used Intent(Context packageContext, Class
cls) constructor with two parameters.
The first parameter is Context. If you remember, when we created View in one of our previous
lessons, we have also used a Context object. Activity is a subclass of Context, that’s why we
can use Activity as a Context object - this. To be short, Context is an object that grants access
to basic application functions such as: access to resources, file system, Activity invocation, etc.
I guess in future we will explore examples where we will see explicitly what Context is used for
and how it is used.
The second parameter is a class name. I will remind that when creating Activity, we specify its
class name in the manifest-file. Now if we specify the same class for Intent - the system will
look up the corresponding Activity in the manifest-file and show it.
Explicit invocation
Invoking Activity with such an Intent is an explicit invocation. That is, using the class
we explicitly specify which Activity we would like to see. It is usually used inside one
application. It can be illustrated in the following way:
We create an Intent here and pass it Class_B class as a parameter. After this we
invoke startActivity method with created Intent as a parameter. Method checks
Implicit invocation
There is also an implicit Activity invocation. The difference is that for creating an Intent we
use not a class object, but fill action, data, category parameters with specific values. The
combination of these values defines a goal which we are trying to accomplish. For example,
sending a letter, opening a link, editing some text, viewing an image, calling a specific number
and so on. By-turn we specify an Intent Filter for the Activity - it is a set of the same
parameters: action, data, category (but the values are its own, they depend on what Activity is
capable of doing). And if the parameters of our intent match the conditions of this filter, the
activity is invoked. But now the search goes through all the Activities in the system.
If several Activities are found, the system gives you a choice, which application exactly would
you like to use. It can be illustrated as following:
package ru.startandroid.develop.p0281intentextras;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Fill in the fields with whatever you wish. I will write John in the First Name field and Smith in
the Last Name field.
Press Submit:
ViewActivity is displayed, it read data from Intent and displayed it on the screen.
You can put not only String data inside Intent. In the list of Intent methods you can see all the
types that putExtra method can receive as a parameter.
I implemented one activity with two layout. In this exercise, I implement the same application,
with same function and presentation, using two activity, HelloAndroid.java and
HelloAndroid_2.java. The activity is switched using Intent, the data is passed using Bundle.
HelloAndroid.java
package com.example.helloandroid3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
private
Button okButton;
Button cancel1Button;
EditText textName;
EditText textPhonenumberIs;
EditText textEmailIs;
EditText textWebsiteIs;
EditText textAddressIs;
startLayout1();
}
intent.putExtras(bundle);
startActivity(intent);
finish();
}
};
HelloAndroid_2.java
package com.example.helloandroid3;
import android.app.Activity;
private
Button backButton;
Button cancel2Button;
TextView nameField;
TextView phonenumberField;
TextView emailField;
TextView websiteField;
TextView addressField;
startLayout2();
CharSequence textPhonenumberIs_value =
bundle.getCharSequence("bPhonenumber");
phonenumberField = (TextView) findViewById(R.id.phonenumber);
phonenumberField.setText("Phone Number: "+textPhonenumberIs_value);
startActivity(intent);
finish();
}
Because we have two activity, so we have to modify the file AndroidManifest.xml, to include
the additional activity.
AndroidManifest.xml