azizbekian
azizbekian

Reputation: 62189

Save state for onPause and onResume

I'm even not sure I've written the title correctly, because this is the first time I feel I have to cope with this methods.

In onCreate(), I make a notification with default sound printing a string. Then, I check to see if I get another String. If it isn't equal to the previous String, I make another notification with sound. That is, when I find that I change my location, I make a notification with sound.

The problem is that I want my app to run when it is in background, but as I pause the app, the notification with sound comes immediately (the system doesn't remember that I've already made a notification for that string, it makes another one). And again, as I open the app, I again get the notification.

I want to make only 1 notification for one location even if I close and open app 100 times.

How can I do this?

Hope I could explain the problem.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.map);
    //here are being called location listeners
    somefunction();
}
public void somefunction(){
//finally, after some operation I get a string
if(newString!=oldString)
    {
        oldString=newString;
        notification(); 
    }
}

public void notification(){
// here I make notification with default audio
}

Upvotes: 0

Views: 4823

Answers (2)

bhekman
bhekman

Reputation: 3277

Use a flag boolean that records whether you have already made the sound. Check that this flag is false before making the sound, set it true if the location changes again. (not sure if I properly understood the question). Eg:

boolean alreadySounded = true;

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.map);

    //here are being called location listeners

    checkLocation();    // call both of these in onResume() as well
    checkSounded();

}

private boolean checkLocation() {
    if(newString!=oldString)
    {
        oldString=newString;
        alreadySounded == false;
    }
}

private boolean checkSounded() {
    if(alreadySounded == false)
        notification();
}

Upvotes: 1

Lucas Arrefelt
Lucas Arrefelt

Reputation: 3929

I dont know if I understand your question quite right but here is code that should help you. This code will make the application play once and then save that value for future sessions of your application, meaning it wont play again unless you save a false value.

Hope it helps!

package stackoverflow.sound;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;

public class PlaySound extends Activity 
{
    private boolean hasSoundBeenPlayed;
    private SharedPreferences settings;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Gets values from last session of your application, check if sound was played!
        // If false no saved value was found (first session of your app)
        this.settings = getPreferences(MODE_PRIVATE);
        this.hasSoundBeenPlayed = settings.getBoolean("hasSoundBeenPlayed", false);

        // your logics.. //

        this.notification();
    }

    public void notification()
    {
        // If sound has been played, do nothing
        if (hasSoundBeenPlayed)
            return;

        this.hasSoundBeenPlayed = true;
        // Play your sound
    }

    // If location was changed, make notifications enabled again
    public void changedLocation()
    {
        this.hasSoundBeenPlayed = false;

        // Do whatever and play again
        this.notification();
    }

    // Called whenever application stops
    protected void onStop()
    {
        super.onStop();

        // Whenever application stops, save the sound boolean for future sessions.
        // If sound has been played (boolean = true), it wont play on any future sessions.
        SharedPreferences.Editor editor = this.settings.edit();
        editor.putBoolean("hasSoundBeenPlayed", this.hasSoundBeenPlayed);
        editor.commit();
     }
}

Upvotes: 3

Related Questions