-2

I know how to call a function inside a fragment to an activity,

(activity as MainActivity).bbtn()

however I am wondering how/can I call this function from another activity? As ideally I want to separate what each activity does and would rather an activity that only handled media player to run these commands. At the moment it is looking like I will need to put a lot of my core logic into the MainActivity which could potentially be a lot of unused code.

RussellHarrower
  • 6,339
  • 20
  • 90
  • 182
  • Don't think this deserves a -1 vote as the question itself isn't bad. However, don't do this, it will lead to memory leaks and lots of code in MainActivity that doesn't belong there. – Joozd May 26 '21 at 08:28
  • @Joozd Thanks for this feedback, I am trying to not put all the code in MainActivity and that is why I am trying to seperate the Music Player from the Main Activity. The problem is the same problem Apple Music app has, and that is that their a lot of navigation in our app, and the music player needs to be able to be accessible from most of the activities. – RussellHarrower May 26 '21 at 09:19
  • @Joozd The solution I have come up with is put the music player in a fragment and ping a global class function that tells if track = track if it does keep playing, but if the track url has changed the fragment player unloads the player and loads in the new item and then plays it. – RussellHarrower May 26 '21 at 09:20
  • I would use a separate player object for that without a UI that is kept statically in your app (eg in a repository). Bit much to explain in a comment on an unrelated question :) – Joozd May 26 '21 at 11:32
  • @Joozd is there away you could link to a question that showcases or an example of a repository as I can’t seem to get ExoPlayer to work in a standalone class with out “context” (onCreate) – RussellHarrower May 27 '21 at 02:09

1 Answers1

0

I highly recommend using repository instead. Because it will help you more for separating tasks.
for what you ask for, you can make the method you want to call static but having a lot of static methods is not a good thing especially it can cause a memory leaks.

Mostafa Onaizan
  • 624
  • 1
  • 4
  • 14