I tried to add some Music in the Background of my Main Menu. I finally achieved it by using extends Service. Now i have another problem:
If the Home Button is pressed, the Service still runs and plays music, it only finishes, if i destroy it. I tried using:
#Override
protected void onPause(){
super.onPause();
service.setAction("model.BackgroundSound");
stopService(service);
}
which works fine, but if i switch from my Main Menu to my Character Selection Activity, it will pause too. But i would like to keep the music going in all Activities.
Is there a way to detect the difference of the App being "on pause" or just the activity?
You can use fragments instead of Activities and it will call your Pause() just for the Single Activity.
If you are using API level 14 and higher you can use registerActivityLifecycleCallbacks() on the Application. You can check and example usage in this SO answer.
This way you can check if any of your app's Activities is visible.
Related
I have 2 activities (audio chat activity & message chat activity). Currently both activities working absolutely fine but now I want to both activity should work simultaneously like a Whatsapp messenger.
In audio chat activity there is a button to go into message chat activity but once message chat activity starts the audio chat activity stop working.
Is there any way where I can switch between these two activities like a Whatsapp messenger.
You should change approach. When you start a new Activity B, Activity A is paused, so it can't work. For your purpose you should use a single Activity and show/hide elements (e.g.: messagebox and audiobox)
There is no way to guarantee that an activity that sits below another activity will never get killed. Android can do this to reclaim memory when needed. It's the way Android was designed to facilitate devices with limited resources.
Instead, if you want to let the user seamlessly switch between text and audio chat, you will need to maintain the UI between those two components in the same activity. The easiest way is to simply have two different sets of views in the view hierarchy for the activity and switch between them by changing their visibility.
I hope this will helpful to developer,may be fresher or senior,I have ActivityA and B,this is my ActivityB class
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intnt=new Intent(getApplicationContext(),ActivityA.class);
startActivity(intnt);
}
please use this step in ActivityB to move back your AcitivityA without killed
And solution is
#Override
public void onBackPressed() {
super.onBackPressed();
}
//I hide these below intent(2lines) in ActivityB,So it works thanks by Daemon....
I want to play audio on lock screen and I know for this I have to use service. But I am little confused So I have following questions.
I want to know on home and back button press which method is called i.e. onPause or onStop?
If my app running and someone call me and I pick the call then which method is called i.e. onPause or onStop?
On screen lock which method is called?
first onPause() will be called for each of your question and onStop() will be called when another activity comes in foreground and your previous one goes in background.
The Basic difference is that when onStop() called the previous activity in not visible and new activity comes in foreground and overlap the previous one.
On home press: onPause->onStop. On back press: onBackPressed->onPause->onStop->onDestroy
Same as pressing home essentially.
Same as 2.
Have a look at the documentation:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
I want ask like in title. how to move/send activity to background in android without pause it?
for example in this method:
public void onBackPressed () {
//here what i should wrote to back my previous activity and not stop or pause current
}
That is not possible. By definition:
onPause() will be called when the activity no longer is in the foreground from an input standpoint
onStop() will be called when the activity no longer is in the foreground from a visibility standpoint
In Android, When I press back/home button the music stops (that's fine), but when I re-enter to the activity I want to begin/resume playing back the music.
How does Android manage playback of audio when the program goes into the background?
The code given below only works if the activity hasn't been destroyed. If you want to survive when the user presses the back button, you'll have to write the current status of the media player out to a file, and reload it when the activity starts again.
I'm not sure that's the correct behavior though. After the back button is pressed, your application SHOULD terminate, and start up next time in a reset state. I think. If the back button gets used for navigation in your app, the solution is to put up an alert asking of the user REALLY REALLY wants to quit, on the final back button that terminates the app. Override
Activity.onBackPressed to customize the behavior of the back button.
Note also that your activity can be destroyed even in paused state, if the system is looking for more memory, if the screen flips, or if several other high-level system states change. To deal with this case, you need to implement savedInstanceState handling. Implement Activity.onSaveInstanceState, and then look for a non-null savedInstanceState parameter in your implementation of onCreate.
In both cases, you're going to have to implement some method for recreating the media player, preparing the audio again, and seeking to the correct position in the resurrected audio.
Ordinarily, you should include a lot more details and code about your problem, but I just happened to see your other post on a related matter.
You need the following in onResume(). Note, if you are doing mp.start() in onCreate(), you can remove that start as onResume() is called after onCreate().
#Override
protected void onResume() {
if(mp != null && !mp.isPlaying())
mp.start();
super.onResume();
}
start() from MeidaPlayer will start over if stop or never played, or from where paused if it was paused.
add this method,
#Override
protected void onResume() {
super.onResume();
if(mp != null)
mp.start();
}
How to complete the application when pressed buttons Home and Back? Provided that in my memory holds many pictures.
When pressed on the Back button - application restarts... When pressed on the Home button - quit application, but when it restart - does not start Splashstsreen.
Hard to see without code but it sounds like your activity is resuming rather then starting from scratch (as it should behave). It sounds like it's behaving correctly in that case. If you insist in wanting your application to truly quit after the back button is clicked perhaps you can override onBackPressed() then have your Activity call its finish() method.
I don't think it's good programming practice to fiddle and interfering with the activities lifecycle. It's the OS responsibility to manage the lifecycle, including pause and finish activities.
Instead you should use other methods to handle your problem with not showing splash screen, these methods are onResume and maybe also onStart(). Also you should get familiar with the activity lifecycle(link submitted by #ss1271).
Press Home will let the activity to enter onPause(). however, if you insist to quit the application when press HOME, which is obviously not a good practise, you can do like this:
Override onPause() and onBackPressed()
add finish(); to these methods.
I am sure by adding finish(); to onBackPressed() will quit the app when Back button pressed, but I haven't tried on Home.
Please refer to Activity Lifecycle for more info.