I have a music player
app I created, while Im playing music and exit the app by pressing home or back button and then launch the app again, it doesn't return to the current state where I exit the app and also the song index goes back to its default value which is 1 but the music is still playing. and when I play music again, the current song I played is still playing. therefore, there are two songs are playing. How can I save the current state of my application?
You can save the states in onPause() and can retrieve them back in onresume()
Or you can use onwindowFocusChanged() :
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus)
//retrive state
else
//save state
}
Short answer: You really oughta need to read this activity page which tells you about the life cycle of an activity..
Long answer : what's going on is all your variables are reloading (probaly through onResume or onStart) so you lose state. You can easily save the state of some variable using onSaveInstanceState(Bundle b) link
Then reload your variables using onLoadInstanceState.. Easy as that..
More details can be found in this answer;
https://stackoverflow.com/a/151940/137404
es you can save your current state . just add the following code in your menifest file .
<activity android:name=".Song_list"
android:configChanges="keyboardHidden|orientation|screenSize"
//the line below will solve your problem just put it in every activity that you created
android:launchMode="singleTop">
</activity>
Related
I have scenario where I want to develop an app on TV, in that if user is watching some videos on Youtube and if I want to load my app, then it will launch in foreground and I will display some information with transparent background and behind app will continue to work, like if behind app is Youtube then it should play as it is without pause.
Please help me if it is possible? if yes then how can I achieve that?
In Manifest Define Activity theme as transulent :
<activity android:name=".YourActivity" android:theme="#android:style/Theme.Translucent.NoTitleBar" />
For Transulent Activity life cycle differs from Non-transulent Activity :
For Non-transulent Activity-A if any other Activity-B comes on top of
it then Activity-A will be in onStop state.
Activity-A if any other Activity-B comes on top of it then Activity-A
will be in onPause stateBut where as For Transulent
Then do this :
#Override
public void onPause() {
super.onPause();
if (mVideoView.isPlaying()) {
// Argument equals true to notify the system that the activity
// wishes to be visible behind other translucent activities
if (! requestVisibleBehind(true)) {
// App-specific method to stop playback and release resources
// because call to requestVisibleBehind(true) failed
stopPlayback();
}
} else {
// Argument equals false because the activity is not playing
requestVisibleBehind(false);
}
}
I did implementation it worked go through this it will help u for sure
:
https://developer.android.com/training/tv/playback/options.html
If you requirement is related to Picture in Picture try this :
https://developer.android.com/guide/topics/ui/picture-in-picture.html
I am playing video on video view, the issue which i am facing is that when user press back button, the application gets stuck for some time and then onStop method is called of that activity, i don't know why the application is taking time in calling onStop method. Please can anyone help me out.
What jaydroider said is right. You have to override onBackPressed method as follows
#Override
public void onBackPressed() {
<stop your video here>
super.onBackPressed();
}
It is important that you stop the video before calling super.onBackPressed(); so that your application does not get stuck.
You should stop video on onPause() not on onBackPressed(). Because user may use the Home key while using your app. This will stop playing video whenever your screen is not visible to user.
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();
}
I am developing media player application in which i am showing a notification when song starts playing in a service.
Basically i am showing a list view in my first activity(being any one from Artist tab, album tab etc) and on click of an item , player screen is shown.
I have to implement a back button in my player activity, i guess i just have to do a finish() on click of it.
But in case my previous activity is destroyed and i am coming through pending intent(through notification), if i call finish() now the activity is simply destroyed. In such case i want to recreate the previous activity.
Example :
Album > player screen is shown and songs starts in service with status bar notification..
Suppose i press back now and destroy all the activity.
Click on notification > Player is shown again.. if i press back now Album activity should be shown similarly for artist tab.
So far i am just doing:
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
I guess you can achieve what you need with setting the intent flags accordingly. The best introduction to these flags I found is the following
Android Activities and Tasks series – Intent flags
It is still a confusing subject for me yet the Android application offered along with this article is very helpful to check quickly what the different flags do.
I thinking you must set more informations about song in Service like a album and when you press back in song activity you should chek service and give back it from Service and init album activity
I need to Code a button in the Main menu which can resume the game from the state where it was stopped.
In your activity onPause method, save the state of your game to a file (or a SQLite).
In your activity onResume, check if there is saved state, and if there is one, add one more entry to your main menu. Upon pressing that menu item, read the game state from the file and restore it in memory.
You can read on the various storage options in the Data Storage page.
Save the state every 10 seconds while playing.