我正在尝试为Android平台制作应用程序。到目前为止,我有两项活动。两者都有碎片。目前的第一项活动是使用Facebook api,以便用户可以登录。我希望我的第一个活动在登录完成后切换到第二个活动。我尝试使用带有intent的onClickListener,但第二个活动将在登录完成之前启动。我有意识地将我的代码恢复到没有onClicklistener或intent的程度。任何帮助,将不胜感激。我确实尝试阅读意图和活动的文档,但我无法弄清楚如何做我想要的。我会发布我的代码。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下一个
package com.tfs.taylor.retreat;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.AccessTokenTracker;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.ProfileTracker;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
/**
* A placeholder fragment containing a simple view.
*/
public class MainFragment extends Fragment {
private TextView mTextDetails;
private AccessTokenTracker mTokenTracker;
private ProfileTracker mProfileTracker;
private CallbackManager mCallBackManager;
private FacebookCallback<LoginResult> mCallBack= new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayWelcomeMessage(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
public MainFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallBackManager=CallbackManager.Factory.create();
mTokenTracker=new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
mProfileTracker=new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldprofile, Profile newprofile) {
}
};
mTokenTracker.startTracking();
mProfileTracker.startTracking();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
private void displayWelcomeMessage(Profile profile) {
if (profile != null) {
mTextDetails.setText("Welcome " + profile.getName());
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile");
loginButton.setFragment(this);
loginButton.registerCallback(mCallBackManager, mCallBack);
mTextDetails=(TextView) view.findViewById(R.id.text_details);
}
@Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
displayWelcomeMessage(profile);
}
@Override
public void onStop() {
super.onStop();
mTokenTracker.stopTracking();
mProfileTracker.stopTracking();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallBackManager.onActivityResult(requestCode,resultCode,data);
}
}
答案 0 :(得分:0)
您必须在onSuccess方法中调用新活动。这样,只有在用户成功登录后才会调用您的活动。
private FacebookCallback<LoginResult> mCallBack= new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayWelcomeMessage(profile);
//call new activity here.
}
&#13;