michael
michael

Reputation: 110710

Try to understand "Calling startActivity from outside of an Activity context"

Need help in understanding this exception:

   system_log_all 11-14 11:52:28.540 E/AndroidRuntime(31615): FATAL EXCEPTION: main
system_log_all 11-14 11:52:28.540 E/AndroidRuntime(31615): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
system_log_all 11-14 11:52:28.540 E/AndroidRuntime(31615):  at android.app.ContextImpl.startActivity(ContextImpl.java:689)
system_log_all 11-14 11:52:28.540 E/AndroidRuntime(31615):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
system_log_all 11-14 11:52:28.540 E/AndroidRuntime(31615):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)

What is the meaning from 'outside of an Activity context'? I don't think I call 'startActivity' from 'Application' Context, so what does it mean by outside of an activity Context?

Thank you.

Upvotes: 5

Views: 14950

Answers (3)

Rathakrishnan Ramasamy
Rathakrishnan Ramasamy

Reputation: 1612

I know it's little bit late response.. But just now I came out from same issue. so I would like to post this on public, it may helpful for others who is on same situation. I just used Myclassname.this instead of getApplicationContext(); .

Upvotes: 2

Olof_t
Olof_t

Reputation: 768

To clearify the previous answer, you need to add the following to the intent before starting the activity:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Then Android will let you start the activity where it pleases you.

Upvotes: 8

tidbeck
tidbeck

Reputation: 2418

As @thinksteep commented is seem like you are trying to call startActivity() from something else then an Activity.

Could it be that you are trying to start the activity from a Service?

It this is what you are trying to do you should follow the advice in the warning and add FLAG_ACTIVITY_NEW_TASK to your intent flags.

The reason for the warning as I see it is that you are trying to start a UI component from something that is not it self a UI component. Most of the time this is not want you want from a usability perspective.

Exceptions could be incoming calls etc.

Upvotes: 5

Related Questions