-2

I make an application with Linphone Library, I have a code to throw an exception in MainActivity like this :

private static MainActivity instance;

public static final boolean isInstanciated() {
    return instance != null;
}

public static final MainActivity instance() {
    if (instance != null) {
        return instance;
    }
    throw new RuntimeException("LinphoneActivity not instantiated yet");
}

Method instance will be used in another activity (IncomingCallActivity and Contact Data Activity)

Incoming Call Activity will launch if there is a call to the phone

if (!LinphoneManager.getInstance().acceptCallWithParams(mCall, params)) {
            // the above method takes care of Samsung Galaxy S
            Toast.makeText(this, R.string.couldnt_accept_call, Toast.LENGTH_LONG).show();
        } else {
            if (!MainActivity.isInstanciated()) {
                return;
            }
            final LinphoneCallParams remoteParams = mCall.getRemoteParams();
            if (remoteParams != null && remoteParams.getVideoEnabled() && LinphoneManager.getInstance().isAutoAcceptCamera()) {
                MainActivity.instance().startVideoActivity(mCall);
            } else {
                MainActivity.instance().startIncallActivity(mCall);
            }
        }

ContactDataActivity will launch if user choose contact menu

private void doCall(String number) {
        Log.d(TAG, "try to call here: " + number);
        if (Account.isPrepaidRegistered()) {
            MainActivity.instance().requestToCall(number);
        } else {
            Intent intent = new Intent(ContactDataActivity.this, PrepaidRegisterActivity.class);
            startActivity(intent);
        }
    }

Where each of that activity need to call instance of MainActivity to call method in MainActivity to requestcall or incall.

some time when running the app, logcat will show :

java.lang.RuntimeException: LinphoneActivity not instantiated yet

Is that just normal, or there is something wrong with my code ?

Redturbo
  • 1,563
  • 8
  • 22
  • 34
  • Where do you call `instance()` method? – Ircover May 12 '15 at 09:29
  • In Another Activity (Incoming Call Activity), like this : if (!MainActivity.isInstanciated()) { return; } final LinphoneCallParams remoteParams = mCall.getRemoteParams(); if (remoteParams != null && remoteParams.getVideoEnabled() && LinphoneManager.getInstance().isAutoAcceptCamera()) { MainActivity.instance().startVideoActivity(mCall); } else { MainActivity.instance().startIncallActivity(mCall); } – Redturbo May 12 '15 at 10:13

1 Answers1

0

Never try to instantiate an activity by your self the android framework does that for you, If you want your Activity to be singleton use lauchmodes in your intent filter not the singleton design pattern there are plenty of good tutorials on lauchMode here, here and here

nvinayshetty
  • 3,187
  • 1
  • 21
  • 32
  • I have put this on My Manifest file : android:launchMode="singleTask", so I must delete MainActivity instance() method ? – Redturbo May 12 '15 at 10:06
  • yes if the single task is what you are trying to acheive – nvinayshetty May 12 '15 at 10:08
  • but I need to call this instance() method in another activity, can you give another solution ? – Redturbo May 12 '15 at 10:29
  • post all relavent code and explain what are you trying to achieve...if you are trying to communicate between activities use local broadcast receivers or some shared data – nvinayshetty May 12 '15 at 10:37