Kotlin Android – Start Another Activity – Exampleandroid start activity

A new version of this course is now available, updated to reflect best practices for more recent versions of the Android framework and Android Studio. The new course is available at Android Developer Fundamentals course Version 2 , or go directly to the new practical Codelabs. This course is now deprecated and this content will be removed from this site soon.

To start a service to perform a one-time operation (such as download a file) by passing an Intent to startService(). The Intent describes the. Here is a simple method to add a back arrow to other Activity to come back to MainActivity. To enter a new activity we create an Intent then start. The startActivity(Intent) method is used to start a new activity, which will be placed at the top of the activity.

Here you should use this as the context, and SecondActivity. When you click the Send button the main activity sends the intent and the Android system launches the second activity. That second activity appears on the screen. To return to the main activity, click the Android Back button at the bottom left of the screen, or you can use the left arrow at the top of the second activity to return to the main activity.

Make this change and run your app. In the last task, you added an explicit intent to the main activity that activated the second activity. You can also use intents to send data from one activity to another. In this task, you'll modify the explicit intent in the main activity to include additional data in this case, a user-entered string in the intent extras.

You'll then modify the second activity to get that data back out of the intent extras and display it on the screen. The new layout for the main activity looks like this:. Your intent object can pass data to the target activity in two ways: in the data field, or in the intent extras.

The intent's data is a URI indicating the specific data to be acted on. If the information you want to pass to an activity through an intent is not a URI, or you have more than one piece of information you want to send, you can put that additional information into the intent extras instead. To pass information from one activity to another, you put keys and values into the intent extra bundle from the sending activity, and then get them back out again in the receiving activity.

The new layout for the second activity looks the same as it did in the previous task, because the new TextView does not yet contain any text, and thus does not appear on the screen. Now that you have an app that launches a new activity and sends data to it, the final step is to return data from the second activity back to the main activity.

You'll also use intents and intent extras for this task. Use these values:. The SecondActivity. You will implement this method in the next task. The new layout for the second activity looks like this:. The main activity needs a way to display the reply sent back from the second activity. In this task you'll add TextViews to the main activity layout to display that reply.

To make this easier, you will copy the TextViews you used in the second activity. The layout for the main activity looks the same as it did in the previous task--although you have added two new TextViews to the layout. However, since you set the TextViews to invisible, they do not appear on the screen. When you use an explicit intent to start another activity, you may not expect to get any data back -- you're just activating that activity.

In that case, you use startActivity to start the new activity, as you did earlier in this lesson. If you want to get data back from the activated activity, however, you'll need to start it with startActivityFromResult. In this task you'll modify the app to start the second activity and expect a result, to extract that return data from the intent, and to display that data in the TextViews you created in the last task.

Now, when you send a message to the second activity and get a reply back, the main activity updates to display the reply. Android Studio project: TwoActivities. When any of those buttons are clicked, launch a second activity.

That second activity should contain a ScrollView that displays one of three text passages you can include your choice of passages. Add the app bar. Control the system UI visibility. Supporting swipe-to-refresh. Pop-up messages overview. Adding search functionality.

Creating backward-compatible UIs. Media app architecture. Building an audio app. Building a video app. Routing between devices. Background tasks. Sending operations to multiple threads. Manage device awake state. Save data in a local database. Sharing simple data. Sharing files. Sharing files with NFC. Printing files. Content providers.

Autofill framework. Contacts provider. Data backup. Back up key-value pairs. Remember and authenticate users. User location. Using touch gestures. Handling keyboard input. Supporting game controllers. Input method editors. Performing network operations. Transmit network data using Volley.

Perform network operations using Cronet. Transferring data without draining the battery. Reduce network battery drain. Transfer data using Sync Adapters. Discover and connect. Runtime API reference. Web-based content. Android App Bundles. Google Play Instant. Get started with instant apps. Get started with instant games.

Integrate with Firebase. Best practices. Building effective unit tests. Automating UI tests. Testing app component integrations. Android Vitals. Optimize Your Game. Optimizing for Battery Life. Build and test apps for accessibility. Android TextView - Justify Text. Android - New Button programmatically.

Android Button - OnClickListener. Android Button - Disable All Caps. Android Button - Custom Background. Android Button - Change background programatically. Android Toast - Example. Android EditText - Create programmatically. Android EditText - Keyboard with only Numbers. Android SeekBar - Example. Android - Start Another Activity.

Android AlertDialog - Example. Android WebView - Example. Kotlin Android - Inderterminate ProgressBar. Android Snackbar - Example. Android Snackbar - Set Action.