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. public class FirstMainActivity extends Activity {
  13. Button b;
  14. EditText user;
  15. EditText pass;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_first_main);
  20. b = (Button) findViewById(R.id.b1);
  21. user = (EditText) findViewById(R.id.t1);
  22. pass = (EditText) findViewById(R.id.t2);
  23. b.setOnClickListener(new OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. // TODO Auto-generated method stub
  27. if (user.getText().toString().equals("Neeraj")
  28. && pass.getText().toString().equals("Neeraj")) {
  29. Toast.makeText(FirstMainActivity.this,
  30. "Successfully Login", Toast.LENGTH_SHORT).show();
  31. Intent i = new Intent(FirstMainActivity.this,
  32. SecondMainActivity.class);
  33. i.putExtra("User", user.getText().toString());
  34. i.putExtra("Pass", pass.getText().toString());
  35. user.setText("");
  36. pass.setText("");
  37. startActivity(i);
  38. } else {
  39. Toast.makeText(FirstMainActivity.this, "Invalid",
  40. Toast.LENGTH_SHORT).show();
  41. user.setText("");
  42. pass.setText("");
  43. }
  44. }
  45. });
  46. }
  47. @Override
  48. public boolean onCreateOptionsMenu(Menu menu) {
  49. // Inflate the menu; this adds items to the action bar if it is present.
  50. getMenuInflater().inflate(R.menu.first_main, menu);
  51. return true;
  52. }
  53. @Override
  54. public boolean onOptionsItemSelected(MenuItem item) {
  55. // Handle action bar item clicks here. The action bar will
  56. // automatically handle clicks on the Home/Up button, so long
  57. // as you specify a parent activity in AndroidManifest.xml.
  58. int id = item.getItemId();
  59. if (id == R.id.action_settings) {
  60. return true;
  61. }
  62. return super.onOptionsItemSelected(item);
  63. }
  64. }
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. }