1

i'm writing a little image uploader app which uses the AsyncTask to upload and notification to let the user know if the upload is completed.

After the upload is finished i want to update the UI using the "onPostExecute"-Method from my AsyncTask-Thread. If you click on the notification it should navigate the user to the main activity where you can see all uploaded pictures(also the newest one).

For that purpose i'm creating the notification in the "onPreExecute"-Method with the intent to come back:

 @Override
    protected void onPreExecute() {
        mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(context)
        .setContentTitle("ImageUpload")
        .setContentText("Uploading in progress")
        .setSmallIcon(R.drawable.ic_launcher);
        mBuilder.setProgress(0, 0, true);
        mBuilder.setAutoCancel(true);
        Intent resultIntent = new Intent(context, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());

    }

The used context is passed when i'm creating the AsyncTask Object:

new ImageUploader(getApplicationContext()).execute((getRealPathFromURI()));

After the upload is finished i'm changing the text of the notification and update the UI (in the moment only one TextView to test it):

    @Override
    protected void onPostExecute(String result) {           
        if(result == null) {
            setNotificationFinshedText("A error occured during the Upload");
        } else {
            setNotificationFinshedText("Upload complete!");
            txt.setText(result);
        }
    }

That works fine, after the "onPostExecute"-Method is finished i have the updated text in my activity and my "Upload complete!"-Notification in the bar.

Now my problem begins: If i click now, my notification opens the application but with the old context text / UI. That means my updated UI text disappears and is replaced with the old one.

For example:

  1. Start App, text is: "placeholder text".
  2. Start Upload, after its fnished the text is: "url: www.website.de/img.png".
  3. Click Notification: Opens Activity, text is again: "placeholder text".

My idea: It's because the returning intent is using my "old" activity context which is set before i change the UI. But i don't know how to solve this problem. Is it possible to set the resultIntent and TaskStackBuilder in the "onPostExecute"-Method after updating the UI ?

4

0 回答 0