Intents in Android

Introduction

 
Intents are requests from an activity to another activity. In Android, activity means a single page for implementing elements on it. An Intent is an asynchronous message that sends values from one component to another component of 2 different activities. It is mainly used for starting an activity from another activity.
 
start activity from another activity
 
Declaration
  1. android.content.Intent intent =new intent(parameters);   
This defines the source to the destination.
 
destination
  1. startActivity(intent);   
This starts the activity to transfer the values and the request to another activity.
 

Types Of Intents

 
Android supports implicit and explicit Intent.
 
Implicit Intent: Specify an action from the current activity to another. It uses built-in applications to perform the task.
  1. Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));    
  2. startActivity(i);   
According to this example when we use this intent then the web browser automatically opens and searches Google. In other words, the current activity transfers the link on the web browser for the search.
 
ACTION_VIEW show action.
 
The following are all the Basic Actions:
  1. ACTION_MAIN  
  2. ACTION_VIEW  
  3. ACTION_ATTACH_DATA  
  4. ACTION_EDIT  
  5. ACTION_PICK  
  6. ACTION_CHOOSER  
  7. ACTION_GET_CONTENT  
  8. ACTION_DIAL  
  9. ACTION_CALL  
  10. ACTION_SEND  
  11. ACTION_SENDTO  
  12. ACTION_ANSWER  
  13. ACTION_INSERT  
  14. ACTION_DELETE  
  15. ACTION_RUN  
  16. ACTION_SYNC  
  17. ACTION_PICK_ACTIVITY  
  18. ACTION_SEARCH  
  19. ACTION_WEB_SEARCH  
  20. ACTION_FACTORY_TEST 
Ther action will be used depending on their format like:
  1. ACTION_VIEW -> Uri.parse(http://www.google.com);  
  2. ACTION_DIAL ->Uri.parse(“tel:123456789”); 
123456789 is contact Number.
 

Explicit Intent

  1. Intent i1 =new Intent(FirstMainActivity.this,SecondMainActivity.class);    
  2. String username="Neeraj";    
  3. String password="Neeraj";    
  4. i1.putExtra("Username", username);    
  5. i1.putExtra("Password", password);    
  6. startActivity(i1);   
This example shows two activities, one is “FirstMainActivity” and the other is “SecondMainActivity”.
 
FirstMainActivity,this” means the current activity that sends values.
 
The “SecondmainActivity.class” means another activity that gets values.
 
So the preceding example shows the transfer of values from the current activity to another activity.
 

How to get Values in another Activity from an Intent?

 
When we use an Intent to send a value from one activity to another then we use a “Bundle”. A Bundle receives a component from previous activity.
Demo Example of Login
 
FirstMainActivity.java file code,
  1. package com.example.logindemo;    
  2. import android.app.Activity;    
  3. import android.content.Intent;    
  4. import android.os.Bundle;    
  5. import android.view.Menu;    
  6. import android.view.MenuItem;    
  7. import android.view.View;    
  8. import android.view.View.OnClickListener;    
  9. import android.widget.Button;    
  10. import android.widget.EditText;    
  11. import android.widget.Toast;    
  12.     
  13. public class FirstMainActivity extends Activity {    
  14.     Button b;    
  15.     EditText user;    
  16.     EditText pass;    
  17.     
  18.     @Override    
  19.     protected void onCreate(Bundle savedInstanceState) {    
  20.         super.onCreate(savedInstanceState);    
  21.         setContentView(R.layout.activity_first_main);    
  22.     
  23.         b = (Button) findViewById(R.id.b1);    
  24.         user = (EditText) findViewById(R.id.t1);    
  25.         pass = (EditText) findViewById(R.id.t2);    
  26.     
  27.         b.setOnClickListener(new OnClickListener() {    
  28.             @Override    
  29.             public void onClick(View v) {    
  30.                 // TODO Auto-generated method stub    
  31.                 if (user.getText().toString().equals("Neeraj")    
  32.                         && pass.getText().toString().equals("Neeraj")) {    
  33.                     Toast.makeText(FirstMainActivity.this,    
  34.                             "Successfully Login", Toast.LENGTH_SHORT).show();    
  35.     
  36.                     Intent i = new Intent(FirstMainActivity.this,    
  37.                             SecondMainActivity.class);    
  38.                     i.putExtra("User", user.getText().toString());    
  39.                     i.putExtra("Pass", pass.getText().toString());    
  40.                     user.setText("");    
  41.                     pass.setText("");    
  42.                     startActivity(i);    
  43.                 } else {    
  44.                     Toast.makeText(FirstMainActivity.this"Invalid",    
  45.                             Toast.LENGTH_SHORT).show();    
  46.                     user.setText("");    
  47.                     pass.setText("");    
  48.                 }    
  49.             }    
  50.         });    
  51.     }    
  52.     @Override    
  53.     public boolean onCreateOptionsMenu(Menu menu) {    
  54.         // Inflate the menu; this adds items to the action bar if it is present.    
  55.         getMenuInflater().inflate(R.menu.first_main, menu);    
  56.         return true;    
  57.     }    
  58.     @Override    
  59.     public boolean onOptionsItemSelected(MenuItem item) {    
  60.         // Handle action bar item clicks here. The action bar will    
  61.         // automatically handle clicks on the Home/Up button, so long    
  62.         // as you specify a parent activity in AndroidManifest.xml.    
  63.         int id = item.getItemId();    
  64.         if (id == R.id.action_settings) {    
  65.             return true;    
  66.         }    
  67.         return super.onOptionsItemSelected(item);    
  68.     }    
  69. }   
SecondMainActivity.java file Code,
  1. package com.example.logindemo;    
  2. import android.app.Activity;    
  3. import android.os.Bundle;    
  4. import android.view.Menu;    
  5. import android.view.MenuItem;    
  6. import android.widget.Button;    
  7. import android.widget.TextView;    
  8. public class SecondMainActivity extends Activity {    
  9.     TextView tv1, tv2;    
  10.     Button b;    
  11.     @Override    
  12.     protected void onCreate(Bundle savedInstanceState) {    
  13.         super.onCreate(savedInstanceState);    
  14.         setContentView(R.layout.activity_second_main);    
  15.         Bundle bundle = getIntent().getExtras();    
  16.         b = (Button) findViewById(R.id.fill);    
  17.         String user = bundle.getString("User");    
  18.         String pass = bundle.getString("Pass");    
  19.         tv1 = (TextView) findViewById(R.id.v1);    
  20.         tv2 = (TextView) findViewById(R.id.v2);    
  21.         tv1.setText(user);    
  22.         tv2.setText(pass);    
  23.     }    
  24.     @Override    
  25.     public boolean onCreateOptionsMenu(Menu menu) {    
  26.         // Inflate the menu; this adds items to the action bar if it is present.    
  27.         getMenuInflater().inflate(R.menu.second_main, menu);    
  28.         return true;    
  29.     }    
  30.     @Override    
  31.     public boolean onOptionsItemSelected(MenuItem item) {    
  32.         // Handle action bar item clicks here. The action bar will    
  33.         // automatically handle clicks on the Home/Up button, so long    
  34.         // as you specify a parent activity in AndroidManifest.xml.    
  35.         int id = item.getItemId();    
  36.         if (id == R.id.action_settings)    
  37.         {    
  38.             return true;    
  39.         }    
  40.         return super.onOptionsItemSelected(item);    
  41.     }    
  42. }