I'm building an app that plays music on the background with service. The music is muted when the user is not in the app and unmuted when he is back. I used the onPause() and onResume() methods to do this.
The problem is - every time the user enters a new activity, the music pauses for a second and then resumes, because for a second the current activity is paused.
another problem - for the first run of the activity, the onResume() and onCreate() both works, which makes my service stops from the onResume() without it even being created from the onCreate() (which makes the app shut down).
is there a way to use onPause() without the intent part (or another similar way?)
is there a way to use onCreate() without to onResume() on the first run?
protected void onCreate(Bundle savedInstanceState) {
counter++;
if (counter==1) {
alliesSPEditor.putBoolean("isAllies", true);
alliesSPEditor.commit();
startService(new Intent(this,MusicService.class));
}
#Override
protected void onPause() {
super.onPause();
MusicService.getMP().mute();
}
#Override
protected void onResume() {
super.onResume();
if (counter!=1) //because the counter is still 1, the app won't shut down this way but it will not unmute the music.
MusicService.getMP().unMute();
}
onResume() is always called after onCreate() before the activity starts running
See https://developer.android.com/guide/components/activities/activity-lifecycle
I see some ways to solve this:
the problem is - every time the user enters a new activity, the music pauses for a second and then resumes, because for a second the current activity is paused
The first is you set a flag in the handler that start your new activity and
check in your onPause() method the value of de flag, if false, do nothing othewise mute de music.
public static boolean openOtherActivity = false;
#Override
protected void onPause() {
super.onPause();
if (!openOtherActivity) {
MusicService.getMP().mute();
}
}
You can use Preferences too if you preferrer
To resolve de onCreate() and onResume() problem, you can use the same logic
creates a flag for the services started and control when you needs unmute the music in anothers methods
public static boolean isServiceCreated = false; // In your activity that start the service
protected void onCreate(Bundle savedInstanceState) {
counter++;
if (counter==1) {
alliesSPEditor.putBoolean("isAllies", true);
alliesSPEditor.commit();
startService(new Intent(this,MusicService.class));
isServiceCreated = true; // set the flag to true
}
}
OnResume() method you can check if the service is started an create your logic
Related
I am updating the local database in onDestroy() of activity A. Activity B is depends on the local DB which is updated in onDestroy() of activity A.
My problem is that whenever I start Activity B and finish Activity A onDestroy() of activity A is being called after onCreate() of Activity B. Due to this issue I am losing the data stored after destroying Activity A.
How do I fix this issue?
Activity A
#Override
protected void onCreate(Bundle savedInstanceState){
//After retrieving User data
//inside onclicklistener
someButton.setOnClickLister( new View.OnClickListener() {
public void onClick(View view) {
finish();
startActivity(this,FamilyInfoActivity.class);
}
}
}
#Override
protected void onDestroy(){
localJson.setStatus(status);
localDBUtil.setLocalJson(this,localJson,connectionId);
super.OnDestroy();
}
Activity B
#Override
protected void onCreate(Bundle savedInstanceState){
localJson = localDBUtil.getLocalJson(this,connectionId);
}
You cannot rely on the timing of onDestroy() to save your changes. You should save changes in onPause() which is the only lifecycle method that is guaranteed to be called.
Also, if you want to pass data from ActivityA to ActivityB, you can use one of the following methods:
Store data in a file
Store data in an SQLite database
Store data in SharedPreferences
Put the data in "extras" in the Intent you use to launch ActivityB (only if the amount of data is not too large)
Please remove finish() in onCreate. Finish is destroying your Activity that's why onDestroy is being called
I've implemented something similar to Android notification of screen off/on, but it's not working the way it should. All I want to do is stop music when the screen is turned off. I created a screen action class like this
public class ScreenAction extends BroadcastReceiver {
public static boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// AND DO WHATEVER YOU NEED TO DO HERE
wasScreenOn = true;
}
}
}
Then, in my main activities on create I have this
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenAction();
registerReceiver(mReceiver, filter);
In my main activities onPause, I have something like this:
public void onPause() {
super.onPause();
if (ScreenAction.wasScreenOn) {
final MediaPlayer mp = MediaPlayer.create(this, R.raw.pcmouseclick1);
mp.setVolume(.1f, .1f);
mp.start();
if (buttonState) {
mServ.reduceVolume();
}
}
}
I found this from an online source, but I am having issues. It seems that the screen state is always set as true, and I'm not sure how to change this.
How do I utilize this ScreenAction class to turn off music in on Pause ONLY when the user has locked the screen? I feel like I am missing something in the onPause because in onCreate I link to the class.
#Override
protected void onPause() {
// when the screen is about to turn off
// or when user is switching to another application
super.onPause();
}
#Override
protected void onResume() {
// only when screen turns on
// or when user returns to application
super.onResume();
}
Here you can see the entire lifecycle of an Activity in android:
Activity Lifecycle
Maybe you could also start and stop your music directly from the receiver:
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// Pause music player
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// Resume music player
}
}
You're checking ScreenAction.wasScreenOn in onPause, but that's happening before the BroadcastReceiver is called to notify you that the screen is being turned off. So at that point, ScreenAction.wasScreenOn is still true, and then it's set to false, but onPause has already run, so your music never gets paused.
To solve this, you should take the action directly in response to the screen turning off in the BroadcastReceiver. If you need to interact with your UI, consider a solution like LiveData as an abstraction so that you're not reliant on your Activity being paused at the exact moment the screen is turned off (consider also that the onPause solution wouldn't work if your Activity weren't currently visible).
I have kind of a game with 2 activities: Start Activity (with high score, start button and tutorial button) and main game activity which is is based on countdown timer, when time is up, game returns to start activity.
Problem is when user starts game and then hits home button and goes back to home screen (just leaving game by home button) Everything is ok until time is up, then Start activity appears on screen with lost message.
I've tried various combined methods like onPause witch finish(); inside and so on but it doesn't work or causes app force close.
I can't handle home button click like in onBackPressed() which I did in that case.
Is there any way to suspend app and pause all threads while it isn't in foreground?
I suggest two way
1
Create global variable like
private isInForgrand = false;
And in onStop() or onPause() and onResume() change it
#Override
public void onStop() {
isInForgrand = false;
super.onStop();
}
#Override
public void onResume() {
super.onResume();
isInForgrand = true;
}
And in onFinish() check it
#Override
public void onFinish() {
if(isInForgrand){
//do what you want
}else{
//your app NOT in Forgrannd
}
2
You can cancel CountDownTimer in onStop()
#Override
public void onStop() {
super.onStop();
mCountDownTimer.cancel();
}
The onStop method and onPause method is called whenever the home button is pressed and when calling setContentView. I want to be able to differentiate between the two to when the home button is pressed, the sound stops and when a new view is set, the sound continues. I'm a noob to java, but I have searched for a long time and can't find a solution.
#Override
protected void onResume() {
super.onResume();
if (btnSound.isChecked()){
snd.start();
}
}
#Override
protected void onStop() {
super.onStop();
snd.pause();
}
Well, to answer your question, the time between both the calls between onResume() and onStop() calls, might be so narrow that, you are not able to differentiate. You can try this if you may:
#Override
protected void onPause() {
super.onPause();
snd.start(); // Sound start
// SLEEP 2 SECONDS HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
snd.pause();
}
}, 2000);
}
i want the spash screen to only show when the app has been compltely destroyed not when it is running in the background and resumed
Android's Live-Circle
When your Acrivity is created:
onCreate
onStart
onResume
when your Activity becomes inactive:
onPause
onStop
when it becomes active again:
onRestart
onStart
onResume
and when it's destroyed:
onPause
onStop
onDestroy
Edit: what i would do is, i would define a global boolean for your Main-Activity, 'showSpash' for example, and initialize it as "true". Then, when your 'onCreate'-method is first called, you set it to "false".
Then, anytime the 'onCreate'-method is called, you check if the boolean is "false". If it is, don't show the splash, if not show it.
I created global variable in my application class:
public class MyApplicationClass extends Application {
public static boolean welcomeScreenIsShown = false;
}
Then in my splash activity I did something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//Put this before anything else in OnCreate
if (MyApplicationClass.welcomeScreenIsShown) {
// Open your Main Activity
}
}
Then, in my Main Activity, I did:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyApplicationClass.welcomeScreenIsShown = true;
}
Good Luck!
A very simple method:
Main Activity is only a splash screen. This Activity is shown while a timer starts that elapses for say 4 seconds.
When 4 seconds hits, the splash screen activity is destroyed and the Main Application Activity is started.
Voila, you now have a splash screen that will never be shown, except when you first start the application.
public class SplashScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread t = new Thread() {
public void run() {
try {
int time = 0;
while (time < 4000) {
sleep(100);
time += 100;
}
}
catch (InterruptedException e) {
// do nothing
}
finally {
finish();
Intent i = new Intent(SplashScreen.this, MainApplication.class);
startActivity(i);
}
}
};
t.start();
}
}
A better approach would be to set the android:noHistory="true" attribute for SplashScreenActivity in the AndroidManifest.
Isn't this the purpose of "onResume()" vs. "onCreate()"?