In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
When user clicks on the button, directly inside the XML like that:
Using the attribute
android:onClick
we declare the method name that has to be present on the parent activity. So I have to create this method inside our activity like that:Create an intent to a ViewPerson activity and pass the PersonID (for a database lookup, for example).
Then in ViewPerson Activity, you can get the bundle of extra data, make sure it isn't null (in case if you sometimes don't pass data), then get the data.
Now if you need to share data between two Activities, you can also have a Global Singleton.
Then call it in any activity by:
Easy.
Extras are retrieved on the other side via:
Don't forget to add your new activity in the AndroidManifest.xml:
Start another activity from this activity and u can pass parameters via Bundle Object also.
Retrive data in another activity (YourActivity)
Kotlin
First Activity
Second Activity
Suggestion
Always put keys in constant file for more managed way.
Current responses are great but a more comprehensive answer is needed for beginners. There are 3 different ways to start a new activity in Android, and they all use the
Intent
class; Intent | Android Developers.onClick
attribute of the Button. (Beginner)OnClickListener()
via an anonymous class. (Intermediate)switch
statement. (Pro)Here's the link to my example if you want to follow along: https://github.com/martinsing/ToNewActivityButtons
1. Using the
onClick
attribute of the Button. (Beginner)Buttons have an
onClick
attribute that is found within the .xml file:In Java class:
Advantage: Easy to make on the fly, modular, and can easily set multiple
onClick
s to the same intent.Disadvantage: Difficult readability when reviewing.
2. Assigning an
OnClickListener()
via an anonymous class. (Intermediate)This is when you set a separate
setOnClickListener()
to eachbutton
and override eachonClick()
with its own intent.In Java class:
Advantage: Easy to make on the fly.
Disadvantage: There will be a lot of anonymous classes which will make readability difficult when reviewing.
3. Activity wide interface method using the
switch
statement. (Pro)This is when you use a
switch
statement for your buttons within theonClick()
method to manage all the Activity's buttons.In Java class:
Advantage: Easy button management because all button intents are registered in a single
onClick()
methodFor the second part of the question, passing data, please see How do I pass data between Activities in Android application?