I know how to launch an activity on click of widget button using pending intent but I want to launch a particular method of that activity.
相关问题
- 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
If you need to call a method in another activity then you're following a wrong design. You should not put all of your code in the activities alone.
Why it's a bad choice -
Because you need an object of the class to call a method on it. And how would you get an object of the activity? Unless you are storing the object of one activity into another (That's a pretty messed up thing to do). Another problem in this approach is your other activity might have been destroyed already, so if you're relying on some of the UI element of another activity then you'll get no help at all. Making the activities static will open a huge can of worms for you.
So What's the option available -
There're a lot of options available for doing inter-activity method calls, but I rely on Singletons.They are classes which can have only one object which will be accessed statically, so you won't have to store an object of the class anywhere, since the class itself stores the object. It can go like the following -
So in order to call
someMethod()
from any where in your project you'll just need to callSo do all your calculation in it. You can store the object of current activity in the Manager class or you can abstract out the functionality completely out of the Activity and can get better control over your code. And yes of course you can have more than one singleton classes. I normally have almost 6-7 Singleton managers in my project to handle different tasks.
You need to use the same method in 2 different activites. So, best would to get another Class with that method and then call that method in both the activities.