开发者

how to force restart activity from intent

开发者 https://www.devze.com 2023-03-22 19:31 出处:网络
My MainActivity has the lauchMode=\"singleTask\" Now I want to start the activity from a notification with special intent data.

My MainActivity has the lauchMode="singleTask"

Now I want to start the activity from a notification with special intent data. in MainActivity.onResume I acc开发者_如何学编程ess 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);


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消