Android Intent: Be Careful when Using External Intent Action

Android Intent is defined as a messaging object you can use to request an action from another app component. Intent types could be: implicit and explicit.

Android Intent class

Android intent Action lets a developer utilize System or other user installed applications for tasks such as Capturing Image from Camera, reading Contacts, picking Image from Gallery etc. There are numerous use cases and for a coherent and better User Navigation it is advised to be add flags to prevent user from jumping to that particular activity.

For a real time user, the developer have to  examine use cases carefully and provide proper navigation and function.  For example, when viewing the contact details screen for an entry in the phone address book, a child screen detailing recent posts by the contact on a social network may belong to a social networking application.

When launching another application’s activity to allow the user to say, compose an email or pick a photo attachment, you generally don’t want the user to return to this activity if they relaunch your application from the Launcher (the device home screen). It would be confusing if touching your application icon brought the user to a “compose email” screen.

To prevent this from occurring, simply add the FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET flag to the intent used to launch the external activity, as below:

Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
externalActivityIntent.setType("image/*");
externalActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(externalActivityIntent);

This should make sure the user if left his previous interaction with your application unattended or dismissed the ACTION application is able to start with your application fresh. This should ensures consistency and coherency in your application in your application.

Although, a caveat I can recall if true is: if you set activity launchMode to singleTop in your AndroidManifest.xml then, this solutionmight not work properly. More to that I will publish after testing with samples. Please correct me if I am wrong, write your opinion in comments section about it.

Read more about implementing navigation @developer.android.com

Read more about android intent @developer.android.com

Leave a Reply

Your email address will not be published. Required fields are marked *