Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to force restart activity from intent

Tags:

android

My MainActivity has the lauchMode="singleTask"

Now I want to start the activity from a notification with special intent data. in MainActivity.onResume I access the given intent data...

The problem is: When the activity already exists, and I click on the notification, the activity comes to foreground, but the method onResume is not called and I cannot access the intent data.

I tried the flag FLAG_ACTIVITY_CLEAR_TASK and this works for Honeycomb but not for Gingerbread.

This is how I start the activity from a notification:

Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
intent.putExtra("triggerid", triggerid); 
startActivity(intent);
like image 672
appsthatmatter Avatar asked Jul 26 '11 12:07

appsthatmatter


1 Answers

onResume() is always called if the activity is not in the foreground. However, what you're probably seeing is that getIntent() is returning the intent that started the activity, not the intent that was most recently sent to it.

To fix this, you should override onNewIntent(). This will receive the new intent sent to it. Then you can call setIntent() with the received intent, which will cause getIntent() to return the new intent when used in onResume().

like image 80
blazeroni Avatar answered Nov 14 '22 22:11

blazeroni