从静态方法调用finish() [英] call finish() from static method
问题描述
我正在使用Facebook Android SDK,并希望在用户登录并获取用户对象后关闭我的活动.实际上,我会存储其中的一部分,但是无论如何我都想关闭该活动.
I am using the Facebook Android SDK and want to close my Activity after a user logs in and gets the user object. In practice I am storing parts of it but I want to close the activity regardless.
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
finish(); // causes errors
}
}
});
finish()
上的IDE错误消息是:"Cannot make a static reference to the non-static method finish() from the type Activity"
The IDE error message on finish()
is: "Cannot make a static reference to the non-static method finish() from the type Activity"
如何进行?
推荐答案
在onCreate中创建对您的活动的引用
Create a reference to your activity in onCreate with
//onCreate
final Activity activity = this;
然后您可以在onCompleted回调中使用它
Then you can use that in your onCompleted callback
activity.finish();
您可能必须将Activity activity
设置为全局.
You might have to make Activity activity
global.
编辑2/26/2014:
EDIT 2/26/2014:
请注意,从静态方法调用finish()
可能是不好的做法.您正在告诉Activity
的特定实例及其自身的生命周期,它应该将自己从静态方法中关闭,这种方法没有任何生命周期或状态.理想情况下,您将从绑定了Activity
的对象调用finish()
.
Note that calling finish()
from a static method is probably bad practice. You are telling a specific instance of an Activity
with it's own lifecycle that it should shut itself down from a static method, something without any lifecycle or state. Ideally you'd call finish()
from something with a binding to the Activity
.
这篇关于从静态方法调用finish()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!