0

注销一次后,我无法将 fbid 发送到我的服务器。如果我想再次登录,它总是发送两次请求并开始两次活动。

MainActivity.java:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("TheGaffer", "ONCREATE");
    mFacebook = new Facebook(APP_ID);
    SessionStore.restore(mFacebook, this);
    setContentView(R.layout.login_view);
    mLoginButton = (LoginButton) findViewById(R.id.login);
    SessionEvents.addAuthListener(new SampleAuthListener());
    mLoginButton.init(this, mFacebook);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    mFacebook.authorizeCallback(requestCode, resultCode, data);
}
public class SampleAuthListener implements AuthListener {

    public void onAuthSucceed() {
        new fbRequest().execute("/user_profiles/registerUser");
    }

    private class fbRequest extends AsyncTask<String, Void, String> {

        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(TheGaffer.this , null,
                    "Loading...");
        }

        protected String doInBackground(String... urls) {
            String fbid = null;
            Bundle params = new Bundle();
            params.putString("fields", "id,name");
            try {
                JSONObject jsonObjSend = new JSONObject();
                JSONObject fbData = new JSONObject(mFacebook.request("me", params));
                fbid = fbData.getString("id");
                jsonObjSend.put("fbid", fbData.getString("id"));
                jsonObjSend.put("username", fbData.getString("name"));
                jsonObjSend.put("playerPhoto", "http://graph.facebook.com/"+ fbData.getString("id") +"/picture");
                HttpClient.SendHttpPost(urls[0], jsonObjSend);
            } catch (Exception e) {
                Log.e("FACEBOOK", "Error parsing data " + e.toString());
            }
            return fbid;
        }

        @Override
        protected void onPostExecute(String fbid) {
            Toast.makeText(getApplicationContext(), "Login successful", Toast.LENGTH_SHORT).show();
            progressDialog.dismiss();
            Intent intent = new Intent(TheGaffer.this, TeamActivity.class);
            intent.putExtra("fbid", fbid);
            startActivity(intent);
        }
    }

团队活动.java:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_create_team:
        intent = new Intent(TeamActivity.this, CreateTeam.class);
        return true;

    case R.id.menu_logout:
        Log.i("Logout", "Logged out");
        Intent intent = new Intent(TeamActivity.this, TheGaffer.class);
        startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}
4

1 回答 1

1

Once I was trapped in similar situation where Activity call twice one after another.

To avoid this after lots of research I find a very easy solution.

May be solution is not perfect but works for me

in manifest.xml file

in your activity just add

android:launchMode="singleTask"
于 2012-11-27T05:50:49.550 回答