0

I would like to know how to solve this issue of 'QCAR has already been initialized'. Let's say I want to create Android application that uses QCAR/Vuforia that is done in Unity. The app must be able to traverse between activities including those activities that are used for Vuforia.

Let's call the Activity with QCAR, QCARActivity.java

I know that QCAR can only be initialized once, so when we access QCARActivity.java once, it will be able to load the activity correctly.

Below is the piece of code for QCARActivity:

public class QCARActivity extends NativeActivity {
    protected UnityPlayer mUnityPlayer;
    protected QCARPlayerSharedActivity mQCARShared;        

    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        getWindow().takeSurface(null);
        setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
        getWindow().setFormat(PixelFormat.RGB_565);

        mUnityPlayer = new UnityPlayer(this);

        if (mUnityPlayer.getSettings().getBoolean("hide_status_bar", true))
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);

        final int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
        final boolean trueColor8888 = false;

        mQCARShared = new QCARPlayerSharedActivity();
        mQCARShared.onCreate(this, glesMode,
            new QCARPlayerSharedActivity.IUnityInitializer() {

                @Override
                public void InitializeUnity() {
                    mUnityPlayer.init(glesMode, trueColor8888);
                    View playerView = mUnityPlayer.getView();
                    setContentView(playerView);
                    playerView.requestFocus();
                }
            });
    }

  //Other methods such as onDestroy, onPause, onResume will also be implemented
}

but when we call this activity the 2nd time, it cannot load and will display 'QCAR has already been initialized' in the Logcat.

Is there anyway that we can do to mark that QCAR has already been initialized?

Then if it is already been initialized, how to open the UnityPlayer again so that it wont initialised the QCAR and displays it correctly?

I hope someone can enlighten me on this. Code sample will be appreciated much. Thank you so much.

NOTE: So far I only know how to create Vuforia application through Unity, I used export as Google Project method to extract it to Eclipse compatible java project. I am still not familiar with programming Vuforia directly with Android.

Alvin Tandian
  • 171
  • 1
  • 9

1 Answers1

0

I figure it out myself. Previously, I have added the code mQCARShared.onDestroy() inside QCARActivity.onDestroy() as shown in the code below. This will supposedly de-init any QCAR resources and kills them.

protected void onDestroy ()
{
    mQCARShared.onDestroy();
    mUnityPlayer.quit();
    super.onDestroy();
}

But then I call back to my other Activity through Intent (startActivity(new Intent(GetApplicationContext(), MainActivity.class))) in my QCARActivity class, which is the wrong way to do.

The onDestroy() which supposedly should have destroyed the QCARActivity did not get called. So the right way is to call finish() to return it to the previous activity, which will call the onDestroy() as well. By doing this, we can go back to QCARActivity class and it is still able to initialized the QCAR.

However, when we call finish() , mUnityPlayer.quit() gets called. Then suddenly a weird behavior happens. Your apps quits right away even if your intention was to go back to the previous Activity. This is because inside UnityPlayer.quit() method, it called:

Process.killProcess(Process.myPid()); //This code will kill the process that the app is running.

This can be solved by making the activity that has Unity/Vuforia act as a single process by adding android:process to the manifest, thus killing the Activity instead of the whole application. (e.g. android:process="myProcessName").

Resources: Errors managing the UnityPlayer lifecycle in a native android application

Hope this helps anyone out there.

Community
  • 1
  • 1
Alvin Tandian
  • 171
  • 1
  • 9
  • I Alvin , I have a similar issue , but I don't find any QCARActivity files in my eclipse project. I'll appreciate if you can help. Thanks – m0j1 Feb 02 '15 at 16:59