kirktoon1882
kirktoon1882

Reputation: 1231

In Android create only one instance of a Class

I'm experimenting with PhoneStateListener and I'm having problems being able to turn the Listener off. In my code, I create a new instance of my TeleListener class which is extending the PhoneStateListener. In order to turn the Listener off, I need to call the same instance of my TeleListener Class that I called initially. But the code as I have written it is creating a new instance, not using the existing instance. So my question is, how can I write it so that initially, a new instance is called, then when I want to turn the Listener off, I call the original instance? I'm using a switch to turn the Listener on and off, so here's the switch code as it looks now:

    // ---
    TeleMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    // --- END

    // --- switch
    switch_SW = (SwitchCompat) findViewById(R.id.mainSwitch_SW);
    switch_SW.setTextOn("ON");
    switch_SW.setTextOff("OFF");

    // --- get switch prefs
    switchPref = getSharedPreferences(SWITCH_PREFS, Context.MODE_PRIVATE);
    switch_SW.setChecked(switchPref.getBoolean(switchKeyStr, true));

    switch_SW.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (isChecked) {

                // ---
                TeleMan.listen(tListen, PhoneStateListener.LISTEN_CALL_STATE);
                SharedPreferences.Editor swEditor = getSharedPreferences(
                        SWITCH_PREFS, MODE_PRIVATE).edit();
                swEditor.putBoolean(switchKeyStr, true);
                swEditor.commit();
                Toast.makeText(getApplicationContext(), "ON",
                        Toast.LENGTH_SHORT).show();

                // --- END
            } else {

                // ---
                TeleMan.listen(tListen, PhoneStateListener.LISTEN_NONE);

                SharedPreferences.Editor swEditor = getSharedPreferences(
                        SWITCH_PREFS, MODE_PRIVATE).edit();
                swEditor.putBoolean(switchKeyStr, false);
                swEditor.commit();
                Toast.makeText(getApplicationContext(), "OFF",
                        Toast.LENGTH_SHORT).show();
                // ---
            }

        }
    });// --- END switch

I'm creating the instance of the Listener here:

public class TestActivity extends AppCompatActivity {
private SwitchCompat switch_SW;

SharedPreferences switchPref;
TelephonyManager TeleMan;

public static final String SWITCH_PREFS = "switchPref";
public static final String switchKeyStr = "switchKey";
new instance --> private TeleListener tListen = new TeleListener();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

... and then I go into the above Switch code...

Upvotes: 0

Views: 552

Answers (3)

frogatto
frogatto

Reputation: 29285

I think you need Java Singleton Class Design.

If you have done according this pattern, you would then use it like this:

TeleMan.getInstance().method();
//                      ^--- This could be any methods you've defined
//                           in this singleton.

Note that all references returned by TeleMan.getInstance() point to a same memory location.

TeleMan tm1 = TeleMan.getInstance();
TeleMan tm2 = TeleMan.getInstance();
TeleMan tm3 = TeleMan.getInstance();

tm1 == tm2 == tm3

Upvotes: 1

Christopher Francisco
Christopher Francisco

Reputation: 16278

Even though static would solve the issue, I find that approach as smelly code.

Instead, consider keeping it the way it is and adding the respective logic to attach the listener inside onResume() or onStart() callback; and detaching tListen inside onPause() or onStop() callback methods (depending on the shared preference, of course flag)

Upvotes: 0

kirktoon1882
kirktoon1882

Reputation: 1231

Srinath Ganesh is correct! When I declare the TeleListener variable, I changed it from private to public static like so:

        public static TeleListener tListen;

    ...

    tListen = new TeleListener();
    TeleMan.listen(tListen = new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
    SharedPreferences.Editor swEditor = getSharedPreferences(
            SWITCH_PREFS, MODE_PRIVATE).edit();
    swEditor.putBoolean(switchKeyStr, true);
    swEditor.commit();
    Toast.makeText(getApplicationContext(), "ON",
            Toast.LENGTH_SHORT).show();

Now toggling the switch will enable and disable the Listener Class.

Upvotes: 0

Related Questions