• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps onPause/onStop problems

cr5315

Android Enthusiast
Jul 23, 2010
434
158
Narnia
cr5315.com
I've basically finished my app, when I realized that it would run in the background forever unless killed by the Android system or one of those app killing apps. I tried onStop and onPause with finish() after them, and when I tried that, I got the "Application stopped unexpectedly" error. Then today, when I tried something else, rather than that, the app just closed right away without me pressing home or back.

Here's my code without onStop/onPause
Code:
package com.bb.abbi;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AbbiActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.choose);
        mp.start();
        
        Button one = (Button) findViewById(R.id.button01); 
        one.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
            	MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.love);
                mp.start();
            }})
            ;
        }
		}
 
I've basically finished my app, when I realized that it would run in the background forever unless killed by the Android system or one of those app killing apps. I tried onStop and onPause with finish() after them, and when I tried that, I got the "Application stopped unexpectedly" error. Then today, when I tried something else, rather than that, the app just closed right away without me pressing home or back.

Here's my code without onStop/onPause
Code:
package com.bb.abbi;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AbbiActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.choose);
        mp.start();
        
        Button one = (Button) findViewById(R.id.button01); 
        one.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View v) { 
            	MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.love);
                mp.start();
            }})
            ;
        }
		}

First off, declare MediaPLayer mp; as a class variable and initialize (but don't start) it in the onCreate() method.

Then, override the onStart() method and add mp.start(); to it.

Next, override the onStop() method and include mp.stop(); in it.

Since onStop() is called right before onDestoy(), there is no need to also add it to onDestoy().

All of this is assuming that your problem is spawning from the fact that you are not stoping your media player upon termination.
 
Upvote 0
My problem is not with the media player, it is that the app runs in the background until the Android System decides to close it, but I would like it to "kill itself" when the user presses home or back.

I don't understand how that is possible unless you are using a service. A simple activity will be terminated when the onDestroy() method is called. The onDestroy() method is called whenever home is pressed...
 
Upvote 0
I don't understand how that is possible unless you are using a service. A simple activity will be terminated when the onDestroy() method is called. The onDestroy() method is called whenever home is pressed...

No it isn't. Check the Activity Life Cycle here:

Application Fundamentals | Android Developers

onPause is called when the user moves away from your app.
onResume is called when the user moves back into the app.

onDestroy is called when Android needs to kill off your app. It could do that because you forced it to close in the Settings app, or because the phone is low on resources and things need to be killed off.

It's perfectly possible for an app to be running in the background without being a service.
I've written apps that use threads, and those threads will continue to run even after you move away from the app. I had to handle that by stopping my threads in onPause, and restarting them in onResume.

Mark
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones