有必要每次调用FacebookSdk.sdkInitialize()吗?

时间:2017-01-05 09:54:06

标签: android facebook facebook-sdk-4.0

我在FacebookSdk.sdkInitialize(getApplicationContext())中致电GlobalActivity

public class GlobalActivity extends Application {

    @Override
    public void onCreate() {
       super.onCreate();
        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
    }
}

每次使用facebook sdk时,是否需要再次调用它? 我有一个Fragment LoginButton

public class LoginFragment extends Fragment{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    }
}

1 个答案:

答案 0 :(得分:1)

简单的方法是在Application类中初始化一次就够了。当新活动创建FacebookSdk时自动初始化。

import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
public class ApplicationName extends Application {

@Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}