When calling startActivity, the new activity will live in the same screen stack (context) as the caller. Sometime, the caller may not have an activity context, for instance, starting an activity from Broadcastreceive.onReceive() method, or starting an activity from a service. In these cases, call startActivity will cause an exception of "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK", since the new activity does not have a context to live in.
To fix the issue, setting FLAG_ACTIVITY_NEW_TASK to the intent as follows, then a new context will be created for the new activity.
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Jonathan
Thanks Jonathan..very helpful solved my problem..:)
ReplyDelete"starting an activity from a service" that's what I have in my case. Is there any flag to tell the activity to be closed when home-key is pressed?
ReplyDelete