onPause/onStop problems


Last Updated:

  1. cr5315

    cr5315 Well-Known Member Developer This Topic's Starter

    Joined:
    Jul 23, 2010
    Messages:
    435
    Likes Received:
    158
    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 (Text):
    1. package com.bb.abbi;
    2.  
    3. import android.app.Activity;
    4. import android.media.MediaPlayer;
    5. import android.os.Bundle;
    6. import android.view.View;
    7. import android.widget.Button;
    8.  
    9. public class AbbiActivity extends Activity {
    10.     /** Called when the activity is first created. */
    11.     @Override
    12.     public void onCreate(Bundle savedInstanceState) {
    13.         super.onCreate(savedInstanceState);
    14.         setContentView(R.layout.main);
    15.         final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.choose);
    16.         mp.start();
    17.        
    18.         Button one = (Button) findViewById(R.id.button01);
    19.         one.setOnClickListener(new View.OnClickListener() {
    20.             public void onClick(View v) {
    21.                 MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.love);
    22.                 mp.start();
    23.             }})
    24.             ;
    25.         }
    26.         }
    27.  
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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.
     
  3. cr5315

    cr5315 Well-Known Member Developer This Topic's Starter

    Joined:
    Jul 23, 2010
    Messages:
    435
    Likes Received:
    158
    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.
     
  4. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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...
     
  5. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    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
     
  6. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    I'm not sure why this is a problem? If the operating system needs the resources it will kill it. Are you constantly doing a calculation or something?
     

Share This Page

Loading...