Sinan Cagri Kurt Sinan Cagri Kurt - 5 months ago 8
Android Question

Newbie - I cant learn status of my enum or boolen after my activty gone to background?

I have app where I play one of my three music files. After the activity goes to the background, I reopen this UI activity toggle button comes as "Off" even if I changed its status last time. By the way I read android developer document and it seems pretty complicated.

what should I Do?

package com.example.casperr.sounds;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;


public class Listening extends AppCompatActivity {
public MediaPlayer oursong;
boolean bool ;
private SeekBar mSeekBarVolume;


enum State{
Retrieving,
Stopped,
Preparing,
Playing,
Paused
};
State mState = State.Retrieving;
public AudioManager myAudioManager;



@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.listening);
final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
toggle.setBackgroundColor(Color.parseColor("#26A69A"));

Intent ourintent = getIntent();
int dek1 = ourintent.getIntExtra("dek", 0);
ImageView img = (ImageView) findViewById(R.id.imageView3);
if (dek1 == 1) {
img.setImageResource(R.drawable.a);

} else if (dek1 == 2) {
img.setImageResource(R.drawable.b);

} else if (dek1 == 3) {
img.setImageResource(R.drawable.c);

}
SeekBar seekbar = (SeekBar)findViewById(R.id.seekbar); // make seekbar object
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
float volume = (float) (1 - (Math.log(100 - progress) / Math.log(100)));
oursong.setVolume(volume, volume);
}});

toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if(oursong==null) {
Intent ourintent = getIntent();
int dek1 = ourintent.getIntExtra("dek", 0);
if (dek1 == 1) {

oursong = MediaPlayer.create(Listening.this, R.raw.a);
} else if (dek1 == 2) {

oursong = MediaPlayer.create(Listening.this, R.raw.b);
} else if (dek1 == 3) {

oursong = MediaPlayer.create(Listening.this, R.raw.c);
}
oursong.start();
bool=true;

}else {
toggle.setChecked(true);
}

mState = State.Playing;
} else {
stopPlaying();
}
}
});


}
public void onPause() { super.onPause();
/**oursong.stop();**/}
public void onStart() {
super.onStart();
final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);

if(bool=true){toggle.setChecked(true);}else{toggle.setChecked(false);}

}

private void stopPlaying() {
if (oursong != null) {
oursong.stop();
oursong.release();
oursong = null;
}
}

Answer

You can use sharedPreferences to same the value of that boolean into cache. Do this in the onPause() method

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("MyBoolean", bool_x );
editor.commit();

Then when you Reopen the activity read the boolean from the shared preferences like so

 SharedPreferences sharedPref = context.getSharedPreferences(Context.MODE_PRIVATE);
 boolean myBoolean = sharedPref.getBoolean("MyBoolean" , defaultValue ); 
 // the default value can be anything and is used if the SharedPreferences where empty

also

public void onPause() { super.onPause(); 
       /**oursong.stop();**/} 
public void onStart() { 
    super.onStart(); 
    final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);

    if(bool=true){toggle.setChecked(true);}else{toggle.setChecked(false);}

    //     ^ use " == " not " = " which will always set the value of "bool" to true.
Comments