Monday, July 26, 2010

Android: Intent Switching / Reusing

Recently found this interesting problem in a project where I was using the Intent flag FLAG_ACTIVITY_REORDER_TO_FRONT which is awesome to reuse the same activity kinda of like a cyclic activity switcher, the one issue with this is that if you press back on an activity you have to wait till the activities onDestroy is called. That can be annoying from a UI stand point.

The solution is a combination of flags. The following line fixes the problem.
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
The reason for this it checks the reorder first and reuse or starts a new activity if it needs too.