0

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?

Lamorak
  • 10,957
  • 9
  • 43
  • 57
mohan babu
  • 1,388
  • 2
  • 17
  • 35

3 Answers3

1

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.

Coder55
  • 559
  • 5
  • 17
1

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.

Lamorak
  • 10,957
  • 9
  • 43
  • 57
0

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

lgvalle
  • 3,712
  • 1
  • 19
  • 14