4

I'm using a singleTask-mode activity that handles a certain type of files by specifying a intent-filters. I need to handle each such file Intent exactly once. The problem is onNewIntent() is only called if the task is already alive, forcing me to handle the intent from onCreate() too. Unfortunately, onCreate() gets called for a whole bunch of reasons (e.g. screen rotation), and the Intent returned by getIntent() may be the same one across several onCreate()'s. Of course, it is possible to work-around this using some ugly hack, but I was wondering what the elegant solution would be. So far the best solution I came up with is to setIntent(new Intent(Intent.ACTION_MAIN)) every time after handling an intent. This is a similar pattern to how web servers redirect you to a GET page after a POST page to avoid redoing an operation as result of refresh.

Thanks!

Ytai
  • 41
  • 2

3 Answers3

0

I am not sure If I get your Problem, but once you call an intent and start the new activity, immediately after call finish(); to end the activity you are leaving. This will end your last activity and will prevent from multiple activities from running at the same time.

Also if you are using screen rotation as a way to launch activities, you can always control which one's you do not want to start multiple times by setting some checks using "If and Else" Statements.

0

remove singleTask will solve your problem;
call remove singleTask's activity, can replace with:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
              | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
asfman
  • 471
  • 1
  • 5
  • 9
-1

How about setIntent(null) after handling it?

Jarek Potiuk
  • 19,317
  • 2
  • 60
  • 61
  • Seems to achieve the exact same effect. Is this the proper way to do that? – Ytai Jun 30 '11 at 05:44
  • DO NOT USE THIS METHOD, sometimes it works sometimes it doesn't. Google for a more proper solution, I'm not sure what 100% works all the time, still testing atm. – n3XusSLO Sep 23 '14 at 21:56