Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 846
michael Avatar asked Jan 03 '12 23:01

michael


Video Answer


3 Answers

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.

like image 123
Olof_t Avatar answered Nov 02 '22 23:11

Olof_t


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.

like image 25
tidbeck Avatar answered Nov 03 '22 01:11

tidbeck


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(); .

like image 34
Rathakrishnan Avatar answered Nov 03 '22 00:11

Rathakrishnan