I have a Flash video playing in a WebView. When the user hits the home button, I want to stop the video. I attempt to do this in my Activity:
#Override
protected void onStop() {
Log.i(tag, "onStop");
super.onStop();
this.webView.onPause();
}
I see "onStop" logged out whenever I hit the home button, but the video only stops the first time. I can still hear the audio on all the other times.
The following hack stops the video all the time, but (A) it only stops Flash and not any Javascript timers and (B) it can't shut down any generic Flash video - it can only shut down objects with ID "flashVideo" and with a hook named "stopVideo".
#Override
protected void onStop() {
Log.i(tag, "onStop");
super.onStop();
this.webView.loadUrl("javascript:$('flashVideo').stopVideo();");
}
You could try calling this.webView.destroy() in the onStop() but you would need to reinstantiate the webview and re-add the WebView to the activity programmatically since webView.destroy basically makes it so no other methods may be called on a WebView after destroy.
I had the same issue and i made it work by retaining the state in the BackPressed Method
add the follwing code
mVideoView.stopPlayback();
mCustomViewCallback.onCustomViewHidden();
this might help
Related
how to hide the app content when it is viewed on overview mode?
i have tried to use the onPause() method to call an alert so that it will cover the screen like the code below, but the alert only show after i go back to the app, it does not show the alert while it is on overview
#Override
protected void onPause() {
super.onPause();
openSearch();
}
i expected the onPause() method to cover the app while it is on overview but it is only called after i resume to the app
Add the following in onCreate of activity. This also blocks the user from taking screenshots of the activity.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
I have a multi activity app. In the main activity a service is initiated that plays music. When I navigate through activities the music is still playing ( which is something that I want) but the music is still playing when I click home button and app goes in the background(which is something I don't want).
My first solution was to do 'stopService()' onPause of main activity but this prevented the music from playing in the other activities.
Tried the same in onStop method, same problem occurred.
Q: How can I stop the music(stop the service) from playing when the whole app goes in the background?
My service code:
public class MediaService extends Service {
private MediaPlayer player;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true);
player.start();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
And I start/stop service with:
music_intent = new Intent(this, MediaService.class);
startService(music_intent);
stopService(music_intent);
P.S. Thanks for all the answers but as I said onStop methods stops the music when I change activities which is something that I don't want.
Found this solution in a similar question that uses Application.ActivityLifecycleCallbacks in Application class to check when app goes in Background and then send a Broadcast to the service to stop it.
More in: Stop MediaPlayer service when app goes in Background
According with the Activity lifecicle you should use onStop() to stop your audio from beign played
Remember that onStop()
May never be called, in low memory situations
where the system does not have enough memory to keep your activity's
process running after its onPause() method is called.
So it should solve your problem doing this in this way
#Override
public void onStop() {
super.onStop();
if(player.isPlaying()){
player.stop();
player.release();
}
}
use onStop() or onPause() to stop the service rather than onDestroy();
onStop() - when app goes backgroun(not visible)
onPause() - when any pop-up appears
Here is the actual answer, try this.
For some reason, the service is not stopping when the app goes to background
It stopped long before your stopService() call, as it stopped once onHandleIntent() returned, milliseconds after it was created.
What is not stopping is your Timer, which runs on a background thread and will continue running until you cancel it or your process terminates.
IMHO, this is an inappropriate use of IntentService. If you want to control the lifespan, use a Service, and stop background work in onDestroy().
I know for an activity you can override onStart on onStop methods to know when an activity starts / exits. The issue I'm running into is I want to keep a session open from when a user opens the app until it enters the background / user exits and tying into onStop (for end session) for each activity isn't giving the the results I want, it ends the session every new activity. So I was wondering what are my options for knowing when a user puts the app in the background or exits.
One thing I thought of is keeping track of onStart and onStop (or any two combinations in the activity life cycle) so I know if I have a onStop without a onStart right before the app is exiting. This seems very hacky, not sure if its the right place to start.
Thanks for any input.
You can extend Application class and use registerActivityLifecycleCallbacks to build the logic.
Using for example these two callbacks:
#Override
public void onActivityResumed(Activity activity) {
mBackground = false;
}
#Override
public void onActivityPaused(Activity activity) {
mBackground = true;
}
https://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)
I have a service which plays music when the page is loaded, and carries on to all the other pages as it should. It still played when the application was destroyed, so i put a onDestroy method and an onResume method.
#Override
public void onDestroy(){
super.onDestroy();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
stopService(ServiceMusic);
}
#Override
public void onResume(){
super.onResume();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
startService(ServiceMusic);
}
Now the problem is when the user clicks the home button the service carries on, so I implemented a onPause and it worked fine, just when the user goes to another page the music restarts.
#Override
public void onPause(){
super.onPause();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
stopService(ServiceMusic);
}
How can i overcome this so the music carries on playing when going through the application but when the Home Button is clicked i pause the service?
I would remove the code that is in individual activities and use the registerActivityLifecycleCallbacks mechanism in your application subclass. This will allow you to keep the code in one place and be smarter about starting and stopping your service.
Be aware that with these callbacks, you'll be notified that an Activity has stopped before another activity has started, so you will need some kind of delay when you decide to stop your service. You can use a handler with a delayed runnable to account for this issue.
https://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)
I am using AsyncTask to post some status on social media.
private class PostOnSocialMedia extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
pDialog.setTitle(R.string.posting_status_on_social_media);
Log.d(TAG, "onPreExecuteCalled.");
}
#Override
protected String doInBackground(String... params) {
Log.d(TAG, "doInBackground.");
currentActivity.this.twitter.tweet(params[0]);
currentActivity.this.facebook.post(params[0]);
currentActivity.this.googlePlus.share(params[0]);
return null;
}
#Override
protected void onPostExecute(String result) {
if (pDialog.isShowing()) {
pDialog.dismiss();
}
// Start next Activity when posted on all social media.
Intent intent = new Intent(currentActivity.this,
nextActivity.class);
intent.putExtra("userMatches", userMatches);
startActivity(intent);
Log.d(TAG, "onPostExecute.");
}
}
When user is not logged in to, lets say, Twitter I have to start a new Intent to show twitter page(so that user can log in). This causes the flow to return back to AsyncTask and calls a onPostExecute() method. And new Activity is started.
But I don't want this. What I want is that user login to Twitter and then it will come back to application (when user clicks login flow comes back to current activity. But the activity is created as a new one not the one from which I started a call to social medial post). When it comes back to application, I call a method of Twitter class to proceed to complete the uncompleted posting task. And once it is done, then only I want to go to next activity.
So, is there anyway to handle this situation?
EDIT :
Is it possible to solve this by using onSaveInstanceState(Bundle savedInstanceState) and onRestoreInstanceState(Bundle savedInstanceState)?
before calling asynctask make sure your user is logged in on both platforms, then call asynctask, because now in this case there are lots of combinations of a user can do, so my suggestion is first make sure user is logged in on both facbook, tiwtter etc. then call this asynctask so you dont have to make the asynctask wait
AsyncTask used for background Process.
So you have to check All the condition before calling asynctask and then used.
Generally, AsyncTasks are to do stuff in background without blocking the UI-Thread. If you want your Main-Thread (= UI-Thread) to wait for your AsyncTask, you could just do it without an AsyncTask.
If you want to wait for it though, try yourTask.get();, that should make your Main-Thread wait for the result of your Task. Look here for more info about get().
I solved my problem by using a static variable which is set to true just before recreating an Activity and when Activity execution is in onCreate I use this variable to decide whether this activity is recreated and if yes, then call method to perform remaining task.
When posting is done I call a method of activity, which calls a method to post on social media, from Twitter class where post is successfully done.
Method in activity :
public static void postDone() {
// Go to next activity
}
Call from Twitter class :
MyActivity.postDone();