Start new Activity outside the Activity context.

三世轮回 提交于 2021-02-19 01:39:11

问题


I tried to start an Activity and close other in my AsyncTask class (onPostExecute()).

My code :

Intent i = new Intent(parentActivity, ThunderHunter.class);
c.startActivity(i);
parentActivity.finish();

But it doesn't work, logcat shows :

08-01 18:01:27.640: E/AndroidRuntime(12398): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.app.ContextImpl.startActivity(ContextImpl.java:1029)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.app.ContextImpl.startActivity(ContextImpl.java:1023)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.content.ContextWrapper.startActivity(ContextWrapper.java:283)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at com.radzik.thunter.FunkcjeAPI$Logowanie.onPostExecute(FunkcjeAPI.java:151)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at com.radzik.thunter.FunkcjeAPI$Logowanie.onPostExecute(FunkcjeAPI.java:1)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.os.AsyncTask.finish(AsyncTask.java:631)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.os.Looper.loop(Looper.java:137)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-01 18:01:27.640: E/AndroidRuntime(12398):    at dalvik.system.NativeStart.main(Native Method)

So I changed code to that :

Intent i = new Intent(context, ThunderHunter.class);
c.startActivity(i);
parentActivity.finish();

But without excepted results (still same error).

Is there any way to that properly ?


回答1:


The logcat tells you what the problem is in the first line

Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag

add that flag

Intent i = new Intent(context, ThunderHunter.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(i);
parentActivity.finish();

You can get a list of all available Intent Flags here in the docs




回答2:


Adding Intent.FLAG_ACTIVITY_NEW_TASK will solve your error, but make sure if you need this flag or not as it will trigger the activity as new task which you may not want in your scenario.

In order to avoid this flag, you can write a Handler in main Activity and call it in onPostExecute()




回答3:


try this-

Intent i = new Intent(context, ThunderHunter.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
parentActivity.finish();

You are calling new activity outside the activity context, so you have to set the flag and pass the context outside the activity.



来源:https://stackoverflow.com/questions/17999152/start-new-activity-outside-the-activity-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!