- Starting Another Activity
- This lesson teaches you to
- You should also read
- Respond to the Send Button
- Build an Intent
- Intents
- Create the Second Activity
- Create a new activity using Android Studio
- Create the activity without Android Studio
- Receive the Intent
- Display the Message
- 400+ Android & Flutter Code
- Thursday, January 15, 2015
- How to start another activity from an activity in Android
- Kotlin Android – Start Another Activity – Example
- Kotlin Android – Start Another Activity
- Example – Start Another Activity in Kotlin Android
- activity_another.kt
- Conclusion
Starting Another Activity
This lesson teaches you to
You should also read
After completing the previous lesson, you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some code to MyActivity that starts a new activity when the user clicks the Send button.
Respond to the Send Button
- In Android Studio, from the res/layout directory, edit the activity_my.xml file.
- To the element, add the android:onClick attribute.
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.
In order for the system to match this method to the method name given to android:onClick , the signature must be exactly as shown. Specifically, the method must:
- Be public
- Have a void return value
- Have a View as the only parameter (this will be the View that was clicked)
Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.
Build an Intent
- In MyActivity.java , inside the sendMessage() method, create an Intent to start an activity called DisplayMessageActivity with the following code:
Intents
An Intent is an object that provides runtime binding between separate components (such as two activities). 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. For more information, see Intents and Intent Filters.
Note: The reference to DisplayMessageActivity will raise an error if you’re using an IDE such as Android Studio because the class doesn’t exist yet. Ignore the error for now; you’ll create the class soon.
The constructor used here takes two parameters:
- A Context as its first parameter ( this is used because the Activity class is a subclass of Context )
- The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)
Android Studio indicates that you must import the Intent class.
At the top of the file, import the Intent class:
Tip: In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
Intent specifies the exact app component to which the intent should be given. However, intents can also be implicit, in which case the Intent does not specify the desired component, but allows any app installed on the device to respond to the intent as long as it satisfies the meta-data specifications for the action that’s specified in various Intent parameters. For more information, see the class about Interacting with Other Apps.
At the top of the file, import the EditText class.
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
Assign the text to a local message variable, and use the putExtra() method to add its text value to the intent.
An Intent can carry data types as key-value pairs called extras. The putExtra() method takes the key name in the first parameter and the value in the second parameter.
At the top of the MyActivity class, add the EXTRA_MESSAGE definition as follows:
For the next activity to query the extra data, you should define the key for your intent’s extra using a public constant. It’s generally a good practice to define keys for intent extras using your app’s package name as a prefix. This ensures the keys are unique, in case your app interacts with other apps.
With this new code, the complete sendMessage() method that’s invoked by the Send button now looks like this:
The system receives this call and starts an instance of the Activity specified by the Intent . Now you need to create the DisplayMessageActivity class in order for this to work.
Create the Second Activity
All subclasses of Activity must implement the onCreate() method. This method is where the activity receives the intent with the message, then renders the message. Also, the onCreate() method must define the activity layout with the setContentView() method. This is where the activity performs the initial setup of the activity components.
Create a new activity using Android Studio
Figure 1. The new activity wizard in Android Studio.
Android Studio includes a stub for the onCreate() method when you create a new activity.
- In Android Studio, in the java directory, select the package, com.mycompany.myfirstapp, right-click, and select New > Activity > Blank Activity.
- In the Choose options window, fill in the activity details:
- Activity Name: DisplayMessageActivity
- Layout Name: activity_display_message
- Title: My Message
- Hierarchical Parent: com.mycompany.myfirstapp.MyActivity
- Package name: com.mycompany.myfirstapp
Click Finish.
Open the DisplayMessageActivity.java file.
The class already includes an implementation of the required onCreate() method. You will update the implementation of this method later. It also includes an implementation of onOptionsItemSelected() , which handles the action bar’s Up behavior. Keep these two methods as they are for now.
PlaceholderFragment class that extends Fragment . This activity does not implement fragments, but you might use this later in the training. Fragments decompose application functionality and UI into reusable modules. For more information on fragments, see the Fragments API Guide and follow the training, Building A Dynamic UI with Fragments.
You won’t need it for this app.
If you’re developing with Android Studio, you can run the app now, but not much happens. Clicking the Send button starts the second activity, but it uses a default «Hello world» layout provided by the template. You’ll soon update the activity to instead display a custom text view.
Create the activity without Android Studio
If you’re using a different IDE or the command line tools, do the following:
- Create a new file named DisplayMessageActivity.java in the project’s src/ directory, next to the original MyActivity.java file.
- Add the following code to the file:
Note: If you are using an IDE other than Android Studio, your project does not contain the activity_display_message layout that’s requested by setContentView() . That’s OK because you will update this method later and won’t be using that layout.
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 element as shown here.
Note: Your Android SDK should already include the latest Android Support Library, which you installed during the Adding SDK Packages step. When using the templates in Android Studio, the Support Library is automatically added to your app project (you can see the library’s JAR file listed under Android Dependencies). If you’re not using Android Studio, you need to manually add the library to your project—follow the guide for setting up the Support Library then return here.
If you’re using a different IDE than Android Studio, don’t worry that the app won’t yet compile. You’ll soon update the activity to display a custom text view.
Receive the Intent
Every Activity is invoked by an Intent , regardless of how the user navigated there. You can get the Intent that started your activity by calling getIntent() and retrieve the data contained within the intent.
- In the java/com.mycompany.myfirstapp directory, edit the DisplayMessageActivity.java file.
- In the onCreate() method, remove the following line:
- Get the intent and assign it to a local variable.
- At the top of the file, import the Intent class.
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
Display the Message
- In the onCreate() method, create a TextView object.
- Set the text size and message with setText() .
- Then add the TextView as the root view of the activity’s layout by passing it to setContentView() .
- At the top of the file, import the TextView class.
In Android Studio, press Alt + Enter (option + return on Mac) to import missing classes.
The complete onCreate() method for DisplayMessageActivity now looks like this:
You can now run the app. When it opens, type a message in the text field, click Send, and the message appears on the second activity.
Figure 2. Both activities in the final app, running on Android 4.4.
That’s it, you’ve built your first Android app!
To learn more, follow the link below to the next class.
Источник
400+ Android & Flutter Code
Example code for android + flutter app developers.
Thursday, January 15, 2015
How to start another activity from an activity in Android
To create this example we uses Android Studio IDE. The following android example code demonstrate us how can we start another Activity from an Activity. Activity is an Android application component that render a screen with which app users can interact to do some task such as sending or reading an email, take a photo by phone camera, navigating through a map etc. Activity generated window typically fill the full screen but it may be smaller and can be float on top of other windows.
Generally an android app consists multiple Activities. Such as an email app have one activity to send email, another activity to read email etc. So, an activity should contact other activities. Typically Activities are bound to each other. In the Activity stack, one Activity are defined as a main Activity which presented to app user when they launch the app at first time. Each Activity can then start another Activity to perform additional task. When a new Activity start, the previous Activity is stopped but Android system preserve the Activity in a stack (Back Stack). When a new Activity starts its pushed on to the back stack and got user focus.
When a new Activity is start Android system stopped the previous Activity. When an Activity stopped, the specified Activity release any large objects such as network or database connection. When the Activity resume it reacquire the necessary resources and resume specified actions.
To create an Activity, app developer must create a subclass of Activity or an existing subclass of it. Android system call onCreate() method when creating an Activity. Within this method we should initialize the essential components of this Activity. The setContentView() method allow us to define the XML layout for the Activity’s user interface.
We can start another activity from an Activity by calling startActivity() method. We just need to pass an Intent that describe the new Activity which we want to start. The Intent can specify either the exact Activity or describe the type of action we want to perform (in this situation Android system selects the appropriate Activity, which can even be from a different application). Intent also can carry (pass) small amount of data to the new Activity.
We can start an Activity within same application by using class name. Intent putExtra() method add extended data to the Intent.
An Intent is a messaging object that Android developer can use to request an action from another app component. An Intent used to start an Activity, to start a Service and to deliver a Broadcast. There are two types of Intents, those are Explicit Intents and Implicit Intents. Explicit Intents specify the component to start by name (fully-qualified class name). Typically Explicit Intents are used to start a component within same app. Implicit Intents do not name a specific component but instead declare a general action to perform such as play a video.
Источник
Kotlin Android – Start Another Activity – Example
Kotlin Android – Start Another Activity
When the Android Application we are developing has multiple Activities, we should be able to start another Activity from the current Activity. Meaning navigate between activities.
In this tutorial, we will learn how to start an Activity from current Activity, with an example Android Application.
Starting an activity could be triggered programmatically when an event occurs. Some of such events are
- Clicking a Button or any other View
- Downloading a file is completed
- Clicking on an item in the Drawer
? ? Your browser does not support the video tag.
To start new (another) Android Activity from an Activity, follow these steps.
- In the current Activity, create an Intent with current Activity’s context and Next Activity Class passed as arguments. val intent = Intent(this, AnotherActivity::class.java)
- Call startActivity() method with intent passed as argument. Android OS does start the activity mentioned in the intent. startActivity(intent)
Example – Start Another Activity in Kotlin Android
- IDE Used : Android Studio
- Run : Android Application is run on an Android Device running Android 7.0.
In this example, we shall open new activity on button click from current activity. Consider that there are two activities in the Android Application : 1. MainActivity 2. AnotherActivity
AndroidManifest.xml containing these two activities is
AndroidManifest.xml
- MainActivity is the launcher Activity. This activity is displayed when you open this Android Application.
activity_main.xml
MainActivity.kt
activity_another.kt
AnotherActivity.kt
Run this Android Application. Click on “START ANOTHER ACTIVITY” button. The application navigates to Another Activity. The flow of navigation would be as shown in the following video.
? ? Your browser does not support the video tag.
Conclusion
In this Kotlin Android Tutorial – Kotlin Android Start Another Activity, we have learnt to start a new activity on button click. Infact the same code snippet could be used in any event handler just like button setOnClickListener.
Источник