Intents
An Intent is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases:
- Starting an activity
- Starting a service
- Delivering a broadcast
An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.
How an implicit intent is delivered through the system to start another activity:
[1] Activity A creates an Intent with an action description and passes it to startActivity().[2] The Android System searches all apps for an intent filter that matches the intent. When a match is found, [3] the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.
Example to start an activity
Intent intent= new Intent(this, SecondClass.class); startActivity(intent);
Extras
Key-value pairs that carry additional information required to accomplish the requested action. Just as some actions use particular kinds of data URIs, some actions also use particular extras.
Example: send a string to the secondActivity
//In the firstActivity Intent i = new Intent(FirstActivity.this, SecondActivity.class); String strName = "Lab3 is great!"; i.putExtra("STRING_I_NEED", strName);
//In the secondActivity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); Intent intent = getIntent(); Bundle extras = intent.getExtras(); if(extras != null) { String getString= extras.getString("STRING_I_NEED"); } }
You can add extra data with various putExtra() methods, each accepting two parameters: the key name and the value. You can also create a Bundle object with all the extra data, then insert the Bundle in the Intent with putExtras().
When you use an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object.
Activity example from a manifest file
<activity android:name="MainActivity"> <!-- This activity is the main entry, should appear in app launcher --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="ShareActivity"> <!-- For example, here's an activity declaration with an intent filter to receive an ACTION_SEND intent when the data type is text: --> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> </intent-filter> </activity>
Ex 1
Make a new project with 2 activities (MainActivity and FindTheNumberActivity.
Add the seccondActivity (FindTheNumberActivity) in manifest file
Ex 2
MainActivity will be a login Page like in the picture.
Add an action to the button: when clicked, it will check the username and the password field, and if it's correct, it will redirect you to the secondActivity (FindTheNumberActivity).
Make the username and the password be the string student.
Attention !
You get the text from an editText with “getText()” method, and you convert it to string with “toString()” method.
The password and the username field are editText. And the text from the password field will have to be hidden (add android:inputType=“textPassword”)
Ex 3
Make the mini-game like in the picture. If the number is lower, show a toast with the message “Lower”, and if it is higher with the message “Higher”. If the number is correct, reset the number and show another toast with “You won! Try again” message.
Ex 4
In the FindTheNumberActivity, when the button is cliked, change it's color (red↔blue)