JoshuadV
JoshuadV

Reputation: 194

Android Dev: Do things when App get started

im trying to implement a Sync process for my application. The internal sqlite database has to sync with the remote mysql database. That works great, but now i want to implement, that always on app start the sync method gets called. I simply put it in the onCreate() method but now the problem is, that everytime i go back from a further Activity with Intent, the sync method gets called.

Sorry for my bad english, i hope you guys understood.

Thanks in advance, Joshua

Upvotes: 0

Views: 92

Answers (1)

iForests
iForests

Reputation: 6949

Solution 1

You can extend Application, and sync your data in onCreate() in the Application class.

package com.your.package

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // Sync data here
    }
}

Now open AndroidManifest.xml and find <application> tag. Change it to

<application
    android:name=".MyApplication"
    ...>


Solution 2

Create an SyncActivity, make it the first activity when app open. Sync your data here, and after that, open your original activity by

Intent intent = new Intent(SyncActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

The two flags remove this InitActivity from stack, so when users press back button, they will not go back to this activity.

Upvotes: 2

Related Questions