从活动背景以外调用startActivity() [英] Calling startActivity() from outside of an Activity context

查看:254
本文介绍了从活动背景以外调用startActivity()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的Andr​​oid应用程序实施的ListView 。我绑定到这个的ListView 使用 ArrayAdapter 类的自定义子类。内覆盖 ArrayAdapter.getView(...)的方法,我给你一个 OnClickListener 。在的onClick 方法 OnClickListener ,我想推出一个新的活动。我得到异常:

I have implemented a ListView in my Android application. I bind to this ListView using a custom subclass of the ArrayAdapter class. Inside the overridden ArrayAdapter.getView(...) method, I assign an OnClickListener. In the onClick method of the OnClickListener, I want to launch a new activity. I get the exception:

Calling startActivity() from outside of an Activity  context requires the  
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

我怎样才能获得上下文的ListView (当前活动)正在下?

How can I get the Context that the ListView(the current Activity) is working under?

推荐答案

  • 缓存可以通过构造背景下的对象适配器,或
  • 从你的观点得到它。

或者作为最后的手段,

  • 添加 - FLAG_ACTIVITY_NEW_TASK标志来你的意图:

_

myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

编辑 - 我会避免设置标志,因为它会与事件和历史堆栈的正常流动干扰

Edit - i would avoid setting flags as it will interfere with normal flow of event and history stack.

这篇关于从活动背景以外调用startActivity()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆