mohan babu
mohan babu

Reputation: 1448

Why should the "Parse" Object be declared inside the "onCreate" method?

I'm using as a backend for my Android app. One thing that bothered me when i was writing the code is

@Override
public void onCreate() {
    super.onCreate();

    Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);

    ParseObject testObject = new ParseObject("TestObject");
    testObject.put("foo", "bar");
    testObject.saveInBackground();
}

Why should you declare the parseobject in onCreate() method? Can we declare a ParseObject inside the main method that looks something like

public static void main(String[] args) {
    ParseObject testObject = new ParseObject("TestObject");
    testObject.put("foo", "bar");
    testObject.saveInBackground();
}

If i do this the class is not getting created in the backend. So, I'm looking for a specific reason why ParseObject needs to be created inside onCreate() method?

Upvotes: 0

Views: 124

Answers (3)

Lamorak
Lamorak

Reputation: 11137

This is programming, not – if you declare the function main(String[] args) it does not get called.

You have to respect the lifecycle of Application, Activity, and Fragment classes. Typically onCreate() is the first method in the lifecycle that you can access.

In you usually call Parse.initialize() within Application.onCreate() and then to all your actions like queries and object creation on your Activities/Fragments. If you download the sample project you can learn how to use Parse in an Android project.

Upvotes: 1

Coder55
Coder55

Reputation: 559

you have to mention the lifecycle of android applications. Link to Lifecycle

As you can see, the main(String[]) method is not part of it like in java.

The onCreate() method is the first method that is called, so you should do data preparation and parsing in this method.

Upvotes: 1

lgvalle
lgvalle

Reputation: 3762

This is how Android works: onCreate is going to be the first method the Operating System will call to create your Activity.

Take a look at: http://developer.android.com/training/basics/activity-lifecycle/index.html

Upvotes: 0

Related Questions