0

When running the activity the first time, Parte initialization (which is inside the onCreate method) goes well:

Parse.enableLocalDatastore(this); 
Parse.initialize(this, "...", "...");

Then, If I press the back button and enter again in the activity, I get an error:

java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`

Which I do not really understand why, because the Parse.enableLocalDatastore(this); is before Parse.initialize(this, "...", "...");.

Well, Ok. Then I tried to retrieve when the enableLocalDatastore has finihed, with Parse.isInitialized() method, but it is private, so I can't use it (as well as some others Parse variables I could use).

After some time, I found that If I call both methods inside a new Thread it works.

I'd like to know why the error happens and why It was solved with the Thread. Also, is there any better way to do it?

Follows the code (trimmed for the important parts):

public class RegisterActivity extends Activity {

Button linkParse;

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

    linkParse = (Button)findViewById(R.id.linkparse);
    linkParse.setOnClickListener(new LinkParse());
    linkParse.setClickable(false);

    try {
        // Enable Local Datastore.
        Parse.enableLocalDatastore(this);
        Parse.initialize(this, "...", "...");          
    } catch(Exception e){
        Toast.makeText(RegisterActivity.this, "Parse not started.", Toast.LENGTH_SHORT).show();
        linkParse.setClickable(true);
    }

}

class LinkParse implements View.OnClickListener{
    @Override
    public void onClick(View v) {
        Thread thread = new Thread(new StartParse());
        thread.start();
    }
}

class StartParse implements Runnable{
    @Override
    public void run() {
        try {
            // Enable Local Datastore.
            Parse.enableLocalDatastore(RegisterActivity.this);
            Parse.initialize(RegisterActivity.this, "...", "...");
        } catch(Exception e){

        }
    }
}
}
1

1 Answer 1

2

You should invoke these two lines of codes from the application class not from the activity

Parse.initialize(this, "....","....");
Parse.enableLocalDatatore(this);

There is no need to initialized this multiple times and global application state is meant to be in the application class.

Create a class and extend application and initialize your parse code there

public class MyApplication extends Application{
    @Override
    public void onCreate(){
        super.onCreate()
        Parse.initialize(this, "....", "....");
        Parse.enableLocalDatastore(this)
    }
}

After you have created the application class. Add it to your AndroidManifest.xml

<application android:icon="@drawable/icon"
android:label="@stringapp_name"
android:name="MyApplication">
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.