How to keep an activity alive after back button press? - android

I'm developing a chromecast streaming app for Android. In Activity A the user chooses between different playable items and then is moved to Activity B, which holds the Chromecast controls. When the user hits the back button he is presented with Activity A from where he can choose another video while the casting of the first one is still running on the TV. The problem is that after the back button is pressed, Activity B which holds the controls (for example: seek bar with the video progress) is destroyed and there is no way the user can interact with the destroyed controls anymore. Is there a way I can keep Activity B alive? I'm a novice developer and I have no experience with fragments, so I'm looking for a solution which does not involve their usage...

If you want to use the streaming without any UI showing, you need to create a Service and bind to it with your activities. Let the service handle everything. Like this you're not dependent on the UI being shown.

I strongly suggest you read the Cast UX Checklist where this issue is brought up and addressed. The ways to address that are:
Having a persistent controller on each page
Using the Media Router Controller Dialog when casting
When not in the application but casting, using Notification
When on the lock screen, using the Lock Screen media controller buttons
On Android, you can use the CastCompanionLibrary that provides all of these for you with minimal coding.

Related

Use MediaButton to control foreground app (no audio) with audio in background

I want to use the media buttons to control a foreground app, but not for audio use.
So the goal is to detect button clicks to do certain things in the app.
I can achieve that by using MediaSession and the MediaButtonReceiver. see here
The problem is that when the app is used, often users play music in the background, so the audio focus of the background app takes over the MediaSession and i cannot control my app anymore.
Is there a way to achieve that? Directly listening for button clicks with onKeyDown does not seem to work.
sadly there is no way for two active MediaSessions at the same time. if another app is playing music and run MediaSession then yours doesn't have focus and isn't active... this is made for purpose - better UX - only one "player" app can play music (or video). if this wouldn't work like that and you could play music by few apps at once then how should work media button on e.g. headphones? pasuing/resuming all players? this is just not user-friendly, so Android team introduced MediaSession pattern with option for calling "focus on me now" by any app, but then another app/MediaSession pauses and doesn't get any inputs (this active session does)
if you need physical buttons presses then onKeyDown should work (inside Activity or eventually using AccessibilityService, which would work "globally" in whole system). if you need some on-screen notification buttons presses then just make custom layout for your notification with as much buttons as you like, even styled as a player
note that in Android 11 active MediaSessions notification is stickied to top of notification section when you drop down status bar. your custom notification will be somewhere below between all others (you can manipulate position a bit using priority param for notification/channel)

How are these radio streaming app's functionality implemented?

I've came across some radio app's that share a common, how can I say, way to implement their functionality, and (since I'm new on android dev) I'm really curious about it.
I'll use some images from the app "Simple Radio" to illustrate my doubt.
So, in what I call the main activity, you simply have a feed of radios from where you can choose one to play.
If no radio is playing/stopped on background, what you see when a radio is choosen is this activity, the one I call the streaming activity:
However, if one radio was playing/stopped on background, and you choose another one, what you'll see is this toolbar in the end of the feed:
So.. I know there's a background service for streaming the choosen radio, but how do they manage to control the radio both on the main and streaming activity? Is that possible because services (and it's current state) can be accessed from anywhere or something?
I know there's a background service for streaming the choosen radio, but how do they manage to control the radio both on the main and streaming activity? Is that possible because services (and it's current state) can be accessed from anywhere or something?
The background service is actually a background Service, an Android controller that is independent of activities.
This controller runs until it is told to stop, and has nothing to do what is visually on screen. The visual components can communicate with the Service to send instructions to it and vice versa.

Background Music in App across Activities

I saw lots of similar questions, but I couldn't figure out the best way in my case.
I want to play some background music in my app (looped). It should start (if a sharedPreference is set) at the start of the app. If the user disable a checkbox, it should stop (if he enabled it, it should start again, preferences is set here...).
Nearly everything worked fine by using an MediaPlayer. But I don't know, how to stop the music if the user tabs the home button. Because of playing music across Activities, I think it doesn't seem wise to stop in onPause() or onDestroy()?!
I read a lot about AsyncTask and Services, because I know it's not good to play the music in the MainThread instead of an WorkerThread. But I couldn't figure out the best solution and how to handle it.
Is there a way to play music until I say explicitly "stop" (in any Activity) or the user tabs the home screen button without editing each Activity?

Lock activity on the screen

I have a question about Android Activities.
How to lock current activity on screen and block any navigation/starting new activities?
I have a application, based on WebSocket communication system. And on new transaction started i need to wait for getting any communication result (success or error) and only then i can run new activity or go to previous activity.
But how to implement there? I need to universal method, who will be able to intercept any navigation attempts of standard keys and any navigation attempts of user code.
Thanks.
Actually, I don't think it's possible.
Of course you can override onBackPressed method of the Activity so user will not be able to navigate back. Also you can set FullScreenMode to hide status bar from user.
However, that's very hard to intercept Home key. It's by Android design, so user will always be able to go to the HomeScreen.
So generally, I would recommend using ProgressDialog reference here , you can even setCancelable(false) to prevent it being dismissed by "Back" but "Home" will still minimize your app and go back to the homescreen.

It is possible to make a HoneyComb activity unclosable?

I'm developing a presentation style application for HoneyComb Tablets. At one stage the tablet may be passed around a room for people to interact with. If possible I would like to prevent malicious users from navigating away from the current activity.
So far I have overwritten the onBackPressed() to prevent finishing the activity but users can still press the other buttons on the status bar and also leave the app via notifications that pop up.
Any suggestions or possible solutions?
Thanks
1. Make your activity full screen.
2 Use an alarmManager to trigger your activity from a service on a regular interval say 2or3 second (only if your activity is not foreground). For this use a boolean variable and store it using sharedPreference. this value will be true in onReume and false in onPause or in onStop or in onDestroy. And then only start your activity from your service if the boolean variable is false. Now if your user will press the Home button then AlaramManager kick start your activiy again.
3 Make a special button for finishing your service and activity and for cancel the alarmManager.
As far as I know there is no way to capture the home button press, so this is not possible. Not to mention, it would be a bad ui design decision by the dev team. The home button is there so every Android user has a standard way to exit apps. There would be some extremely malicious apps if there was a way to make the user unable to exit an app.
I'm developing a similar application that runs in a "kiosk" fashion for retail stores. When the application launches, it programmatically hides the system bar so you cannot exit. The system bar is restored when the tablet is rebooted. It requires root/su however.

Categories

Resources