ng2b30
ng2b30

Reputation: 351

Move to other activity when doing AsyncTask

private class MyTask extends AsyncTask<String, Integer, Integer> {
    int number;
    private String content = null;
    private boolean error = false;
    Context mContext;
    int NOTIFICATION_ID = 1;
    Notification mNotification;
    NotificationManager mNotificationManager;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Boolean doInBackground(String... args) {

        return true;
    }

    @Override
    protected void onPostExecute(Integer result) {
        if (response) {

        //Build the notification using Notification.Builder
        Notification.Builder builder = new Notification.Builder(mContext)
                .setSmallIcon(R.mipmap.app_icon)
                .setAutoCancel(true)
                .setContentTitle(contentTitle)
                .setContentText(contentText);

        Intent intent = new Intent(mContext, CaptureActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
                intent, 0);
        builder.setContentIntent(pendingIntent);

        //Get current notification
        mNotification = builder.getNotification();

        //Show the notification
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }
}

The above codes are the part of my asynctask. As the process of the doinbackground would be a bit long, I hope to let user to intent or move to another pages so that they don't have to wait too long and stick with that activity. Is there a way to move to another activity when the doinbackground start? And also if it can pop up the activity when the doinbackgound is finished, it would be great for me. Thank you very much.

Upvotes: 0

Views: 556

Answers (1)

Tomasz Czura
Tomasz Czura

Reputation: 2434

In this situation I suggest to use Service or IntentService instead of AsyncTask - if doInBackground is long, user can go to other activities and this may cause memory leak

Upvotes: 2

Related Questions