I quit the app, relaunch it, I am getting an exception.
public void onCreate() {
-->here Parse.initialize(this, \"adfsfasdfs\",
\"asdfadfsdf\"
Parse.initialize()
should only be called once for an entire application.
Calling it in an Activity
's onCreate
function can cause it to be initialized more than once, as an Activity can be created more than once during an app's lifecycle.
Instead, create an Application class (and add an android:name
attribute to your your application's manifest).
Application: (Note not an Activity/Service/Reciever)
//Note that this is an android.app.Application class.
public class MyApplication extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
//This will only be called once in your app's entire lifecycle.
Parse.initialize(this,
getResources().getString(R.string.parse_application_id),
getResources().getString(R.string.parse_client_key));
}
AndroidManifest:
<application
android:name=".MyApplication">
....
<activity>
....
</activity>
</application>
this is a solution
public class MainActivity extends AppCompatActivity {
static boolean parseIsInit = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!parseIsInit){
Log.d("demo",""+parseIsInit );
Parse.initialize(this, "PutHereYourKeys", "PutHereYourKeys");
parseIsInit=true;
}
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
I solved it using a boolean isParseInitialized variable .Works on orientation change i.e. when activity is recreated in the same Application session . Code snippet :
public class YourActivity extends Activity {
private static boolean isParseInitialized = false;
public static final String APPLICATION_ID = "your_application_id";
public static final String CLIENT_KEY = "your_client_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youractivity);
if(isParseInitialized==false) {
Parse.initialize(this, APPLICATION_ID, CLIENT_KEY);
isParseInitialized = true;
}
..........
}
NEVERMIND, I've fixed it. The problem was due to a syntax error. Thanks to all for solution.
This is weird as I've followed what's given but now I'm not getting any push notifications at all? The only changes I've made:
Manifest
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="full.package.name.UseParse" >
Application class
public class UseParse extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
Parse.initialize(this, "id", "key");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
Check for initialization yourself and just handle the exception and the error will not crash the app just quietly throw the exception.
try {
Parse.initialize(this);
parseinited = true;
}
catch (IllegalStateException e) {
e.printStackTrace();
}