开发者

Launching an Intent outside an activity

I have an asynch task with my app which goes to a website, grabs the results from the API and appends a number of clickable textviews to an existing LinearLayout.

However I want开发者_开发技巧 to be able to launch a new activity when the textview is clicked. This isn't possible with the asynch class defined in a seperate file, would it be easier to define it as an inline class within the activity?


You can always pass Context to your async class.

A better approach would be to have callbacks (listeners) in the calling class for the async to call back to.


One approach is to inflate your TextViews from an XML file that declares an onClick attribute, naming a method defined in your Activity.


Do not use a context as an Activity! You will probably receive a cast error anyway. Instead, you can pass the activity as a function parameter, like this:

 public void function(Activity act)
 {
   Intent intent = new Intent(act, newActivity.class);
   act.startActivity(intent);
 }

Or overload the constructor to accept the activity as a parameter. But I strongly suggest you to check you code. If you are calling an activity, you, probably, should be within another one, don't you agree? But, I Know that sometimes we have to make a few concessions, in order to make things work properly. So, use it wisely.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜