简体   繁体   中英

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.

一种方法是从声明onClick属性的XML文件中扩展TextView,命名在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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM