我有一个facebook初始化sdk调用,启动应用程序时需要它来初始化:
我想使用我的Application类来做到这一点。例如:
public class App extends Application { @Override public void onCreate() { super.onCreate(); FacebookSdk.sdkInitialize(getApplicationContext()); } }
我的主要活动是使用Facebook登录按钮:
public class MainActivity extends AppCompatActivity { @BindView(R.id.login_button) LoginButton loginButton; private CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); callbackManager = CallbackManager.Factory.create(); loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { } @Override public void onCancel() { } @Override public void onError(FacebookException error) { } }); } }
我怎么称呼我的申请单身人士?我如何使其onCreate()工作?
要使您的App类singleton遵循 Singleton设计模式 :
singleton
public class App { // Create the instance private static App instance; public static App getInstance() { if (instance== null) { synchronized(App.class) { if (instance == null) instance = new App(); } } // Return the instance return instance; } private App() { // Constructor hidden because this is a singleton } public void initFBSdk() { FacebookSdk.sdkInitialize(getApplicationContext()); } }
然后在其他任何类中使用它:
App.getInstance().initFBSdk();
如果这是您要的..