All we need in our first activity is a button, which, when pressed, will take us to the next activity. By default, we have the ConstraintLayout set. As we aren’t dealing with a lot of items, we will let it be like this.
In the first line of code, we describe the intent and give it the source and destination activities.
The default settings will name this activity as ‘Main2Activity’ .
This is pretty much the same as the code in the first activity. With just the source and destination activities reversed.
To start an activity, call startActivity () and pass it your Intent. The system receives this call and starts an instance of the Activity specified by the Intent.
The android:onClick attribute’s value, "sendMessage", is the name of a method in your activity that the system calls when the user clicks the button.
To create a new activity using Eclipse: Click New in the toolbar. In the window that appears, open the Android folder and select Android Activity. Click Next. Select BlankActivity and click Next. Fill in the activity details: Project: MyFirstApp. Activity Name: DisplayMessageActivity.
The Intent represents an app’s "intent to do something.". You can use intents for a wide variety of tasks, but most often they’re used to start another activity. Inside the sendMessage () method, create an Intent to start an activity called DisplayMessageActivity:
Because the ActionBar APIs are available only on HONEYCOMB (API level 11) and higher, you must add a condition around the getActionBar () method to check the current platform version. Additionally, you must add the @SuppressLint ("NewApi") tag to the onCreate () method to avoid lint errors.
The android:parentActivityName attribute declares the name of this activity's parent activity within the app's logical hierarchy. The system uses this value to implement default navigation behaviors, such as Up navigation on Android 4.1 (API level 16) and higher. You can provide the same navigation behaviors for older versions of Android by using the Support Library and adding the <meta-data> element as shown here.
All subclasses of Activity must implement the onCreate () method. The system calls this when creating a new instance of the activity. This method is where you must define the activity layout with the setContentView () method and is where you should perform initial setup for the activity components.
This is because the second activity uses the empty layout provided by the template.
The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start.
In the Project window, right-click the app folder and select New > Activity > Empty Activity.
The Context parameter is used first because the Activity class is a subclass of Context. The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start. The putExtra () method adds the value of EditText to the intent.
Your key is a public constant EXTRA_MESSAGE because the next activity uses the key to retrieve the text value. It's a good practice to define keys for intent extras with your app's package name as a prefix. This ensures that the keys are unique, in case your app interacts with other apps.