Introduction

In the previous article, we learned about the View and ViewGroup objects. We learned about Layout and resources as well. In a previous article, we created a hierarchy of View and ViewGroup objects. Now I will develop an activity that shows how to migrate from one activity to another. However, choosing an activity from a new project in Android Studio has already been explained in my previous article.
Creating an activity named MainActivity
What do we need to do? We need to write the code for making an activity just as in a Layout file named activity_main.xml and use the following procedure:
activity_main.xml
This file contains all the attributes of the view elements necessary for making activity. Here we are creating a simple activity that contains only a Text Field (please enter a text) and a button named Send.
Here is the code,
  1. // activity_main.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="horizontal">
  8. <EditText android:id="@+id/edit_message"
  9. android:layout_weight="1"
  10. android:layout_width="0dp"
  11. android:layout_height="wrap_content"
  12. android:hint="@string/edit_message" />
  13. <Button
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="@string/button_send"
  17. android:onClick="sendMessage"/>
  18. </LinearLayout>
    It is just an activity with a Text Field and a Button. Now we attach a navigation-among-activities-using-android-studio:onClick attribute so that the message can be sent and another activity will receive that message.
    String.xml
    String.xml contains the string resources such as "app name" , "edit_message" , "action settings" and so on. So we must remove the default string "hello world".
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3. <string name="app_name">Megatron</string>
    4. <string name="edit_message">please enter a text</string>
    5. <string name="button_send">Send</string>
    6. <string name="action_settings">Settings</string>
    7. <string name="title_activity_main">Main_Activity</string>
    8. <string name="title_activity_child">ChildActivity</string>
    9. </resources>
      It is just an activity with a Text Field with a Button. Now just try to attach a listener of the button so that the message can be sent and another activity will receive that message.
      XML Design
      The previous code produces the following output:
      design of xml part
      Building an Intent
      After creation of the first activity with a TextField and a Button as explained above it is now time to initiate this activity using a thing called an Intent.
      An Intent is an object that provides runtime binding between two separate components (such as two activities). The Intent represents an application's "intent to do something". Intents could be used for a wide variety of tasks, but most often they are used to start another activity.
      In MainActivity.java declare a method named sendMessage(View view) with these parameters and inside it create an Intent to start an activity called DisplayMessageActivity with this code given below:
      1. public void sendMessage(View view) {
      2. Intent intent = new Intent(this, ChildActivity.class);
      3. }
      MainActivity.java will look such as:
      1. package com.example.gkumar.megatron;
      2. import android.content.Intent;
      3. import android.support.v7.app.ActionBarActivity;
      4. import android.os.Bundle;
      5. import android.support.v7.internal.widget.AdapterViewCompat;
      6. import android.view.Menu;
      7. import android.view.MenuItem;
      8. import android.view.View;
      9. import android.widget.EditText;
      10. public class Main_Activity extends ActionBarActivity {
      11. Public final static String EXTRA_MESSAGE="com.example.gkumar.megatron.MESSAGE"
      12. @Override
      13. protected void onCreate(Bundle savedInstanceState) {
      14. super.onCreate(savedInstanceState);
      15. setContentView(R.layout.activity_main_);
      16. }
      17. public void sendMessage(View view)
      18. {
      19. // building an Intent
      20. Intent intent= new Intent(this,ChildActivity.class);
      21. EditText editText = (EditText) findViewById(R.id.edit_message);
      22. String message = editText.getText().toString();
      23. intent.putExtra(EXTRA_MESSAGE , message);
      24. startActivity(intent);
      25. }
      26. @Override
      27. public boolean onCreateOptionsMenu(Menu menu) {
      28. // Inflate the menu; this adds items to the action bar if it is present.
      29. getMenuInflater().inflate(R.menu.menu_main_, menu);
      30. return true;
      31. }
      32. @Override
      33. public boolean onOptionsItemSelected(MenuItem item) {
      34. // Handle action bar item clicks here. The action bar will
      35. // automatically handle clicks on the Home/Up button, so long
      36. // as you specify a parent activity in AndroidManifest.xml.
      37. int id = item.getItemId();
      38. //noinspection SimplifiableIfStatement
      39. if (id == R.id.action_settings) {
      40. return true;
      41. }
      42. return super.onOptionsItemSelected(item);
      43. }
      44. }
        Now the sending and action procedure is completed. Now after clicking the send button a something must be there to catch the text being entered by the user. Then for this we need to create a new Activity.
        Create a second activity
        choosing an activity explicit
        naming this activity blank
        Click Finish and open the ChildActivity.java file and use the following procedure:
        Receive the Intent
        As we all know and learned above that every activity needs an Intent irrespective of the user's handling of Activity.
        Display the Message
        ChildActivity.java
        1. package com.example.gkumar.megatron;
        2. import android.content.Intent;
        3. import android.support.v7.app.ActionBarActivity;
        4. import android.os.Bundle;
        5. import android.view.Menu;
        6. import android.view.MenuItem;
        7. import android.widget.TextView;
        8. public class ChildActivity extends ActionBarActivity {
        9. @Override
        10. protected void onCreate(Bundle savedInstanceState) {
        11. super.onCreate(savedInstanceState);
        12. // receiving the intent
        13. Intent intent = getIntent();
        14. String message = intent.getStringExtra(Main_Activity.EXTRA_MESSAGE);
        15. // display the message
        16. TextView textview = new TextView(this);
        17. textview.setTextSize(50);
        18. textview.setText(message);
        19. // set the text view as the activity layout
        20. setContentView(textview);
        21. }
        22. @Override
        23. public boolean onCreateOptionsMenu(Menu menu) {
        24. // Inflate the menu; this adds items to the action bar if it is present.
        25. getMenuInflater().inflate(R.menu.menu_child, menu);
        26. return true;
        27. }
        28. }
        Output
        After executing the preceding code I am getting the following errors:
        errors android studio
        Although I am working on it I hope the output will be posted very soon.

        Summary

        In this article, we learned how to migrate from one Activity to another Activity using Intents. In Android, everything on the screen that is user intractable is called an activity. This article explains the entire procedure very clearly. However, it includes nearly three major steps to do as shown, for example creating an activity, building intent and creation another activity for receiving the pushed text.