skip to Main Content

I have a main activity that displays login option, if the user clicked login with FB button, I will call fblogin();, and if the login success then I will do intent to open home activity.

right now, the home activity seems to open twice. (i can see it appear twice, stacked)

private void Fblogin()
{
    callbackmanager = CallbackManager.Factory.create();

    // Set permissions
    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile, email, user_birthday,user_friends"));

    LoginManager.getInstance().registerCallback(callbackmanager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {

                            Log.d("LoginActivity", response.toString());
                            Log.d("LoginActivity", object.toString());

                            String jsonresult = String.valueOf(object);
                            System.out.println("JSON Result" + jsonresult);
                            String str_firstname=null,str_id=null;


                            try {

                                str_firstname=object.getString("name");
                                str_id = object.getString("id");
                                String str_email = object.getString("email");

                                Intent home = new Intent(MainActivity.this , HomeActivity.class);
                                home.putExtra("name", str_firstname);
                                home.putExtra("URL", "https://graph.facebook.com/" + str_id + "/picture?width="+PROFILE_PIC_SIZE+"&height="+PROFILE_PIC_SIZE);
                                startActivity(home);


                            } catch (JSONException e) {
                                e.printStackTrace();
                                Log.d("xxxx","aa");
                            }



                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday");
            request.setParameters(parameters);
            request.executeAsync();


        }

        @Override
        public void onCancel() {

            Log.v("LoginActivity", "cancel");
        }

        @Override
        public void onError(FacebookException exception) {

            Log.v("LoginActivity", exception.getCause().toString());

        }
    });
}

in my main activity, i call this in my on create FacebookSdk.sdkInitialize(getApplicationContext());

, iI also check for initial login status `

if ( AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile()!=null ) {

                Intent home = new Intent(MainActivity.this , HomeActivity.class);
                startActivity(home);}

but I think that initial check has nothing to do with it becauseI tried to delete it too but it still happening.`

and in my Home Activity, I have not write any facebook related codes yet.

EDIT : I PUT WHOLE CODE (MainActivity)

import ...

public class MainActivity extends AppCompatActivity implements View.OnClickListener  {


        public static Boolean IsLoggedFB = false; //general state of fb logged
        public static Boolean IsLoggedManual =false; //status boolean if logged in by email (manual)
        public static Boolean IsLoggedGM = false; //general state of google gmail logged


        String  ID_HNBS;  //IDhnbs created when first Registered
        String  Email;

        TextView Fpassword;
        Button Daftar;
        EditText email, password;
        Button LoginEmail;
        LoginButton fb_button;
        SignInButton gplus_button;
        MainActivity myContext;
        static String personName;
        private boolean mIntentInProgress;
        FragmentManager fragmentManager;
        private CallbackManager callbackmanager;

        //for G+
        private GoogleSignInOptions gso;
        private static final int PROFILE_PIC_SIZE = 30;
        private GoogleApiClient mGoogleApiClient;
        private ConnectionResult mConnectionResult;
        private boolean mSignInClicked;
        static final int RC_SIGN_IN = 0;

        /* Is there a ConnectionResult resolution in progress? */
        private boolean mIsResolving = false;

        LinearLayout tv;
        /* Should we automatically resolve ConnectionResults when possible? */
        private boolean mShouldResolve = false;
        public static final String MyPREFERENCES = "xpp";
        SharedPreferences sharedpreferences;
        @Override

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getApplicationContext());
            setContentView(R.layout.activity_main);

            sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();


            IsLoggedManual = sharedpreferences.getBoolean("IsLoggedManual", false); // get value of last login status
            IsLoggedGM = sharedpreferences.getBoolean("IsloggedGM", false); // get value of last login GM


            Daftar = (Button) findViewById(R.id.buttonDaftarEmail);
            LoginEmail = (Button) findViewById(R.id.buttonLoginEmail);
            fb_button = (LoginButton)findViewById(R.id.fblogin_button);




            //Initializing google signin option
            gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestEmail()
                    .build();


            gplus_button = (SignInButton) findViewById(R.id.sign_in_button);
            gplus_button.setSize(SignInButton.SIZE_STANDARD);
            gplus_button.setScopes(gso.getScopeArray());

            //Initializing google api client
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    //.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();



            Daftar.setOnClickListener(this);
            LoginEmail.setOnClickListener(this);
            fb_button.setOnClickListener(this);
            gplus_button.setOnClickListener(this);

            OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
            if (opr.isDone())
            {
                // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
                // and the GoogleSignInResult will be available instantly.
                Log.d("TAG", "Got cached sign-in");

                GoogleSignInResult result = opr.get();

                handleSignInResult(result);

            }
            Log.d("TAG", "ACCESS TOKEN STATUS" + AccessToken.getCurrentAccessToken() + " --- profile=" + Profile.getCurrentProfile());

            //CHECK IF ALREADY LOGGED BY FB
            if ( AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile()!=null ) {
                //load profile and skip (loginfragment) to Home page
                Intent home = new Intent(MainActivity.this , HomeActivity.class);
                startActivity(home);

            } else if ( IsLoggedManual ) {   //IF already LOGGED IN MANUAL (SHAREDPREF)
                Intent home = new Intent(MainActivity.this , HomeActivity.class);
                startActivity(home);

            }
        }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.buttonDaftarEmail) {

            Intent intent = new Intent(MainActivity.this, UserRegistration.class);
            startActivity(intent);
        } else if (v.getId() == R.id.buttonLoginEmail) {

            Intent intent_signin = new Intent(MainActivity.this, LoginManual.class);
            startActivity(intent_signin);


        } else if (v.getId() == R.id.fblogin_button) {


            Fblogin();

        } else if (v.getId() == R.id.sign_in_button) //google sign in button
        {
            Intent intent_Gsignin = new Intent(MainActivity.this, GSignIn.class);
            startActivity(intent_Gsignin);
        }

    }

    private void onSignInClicked() {
        // User clicked the sign-in button, so begin the sign-in process and automatically
        // attempt to resolve any errors that occur.

        mShouldResolve = true;
        mGoogleApiClient.connect();



    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onStop() {
        super.onStop();

    }


    private void Fblogin()
    {
        callbackmanager = CallbackManager.Factory.create();

        // Set permissions
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile, email, user_birthday,user_friends"));

        LoginManager.getInstance().registerCallback(callbackmanager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                // Application code
                                Log.d("LoginActivity", response.toString());
                                Log.d("LoginActivity", object.toString());

                                String jsonresult = String.valueOf(object);
                                System.out.println("JSON Result" + jsonresult);
                                String str_firstname=null,str_id=null;


                                try {

                                    str_firstname=object.getString("name");
                                    str_id = object.getString("id");
                                    String str_email = object.getString("email");

                                    Intent home = new Intent(MainActivity.this , HomeActivity.class);
                                    home.putExtra("name", str_firstname);
                                    home.putExtra("URL", "https://graph.facebook.com/" + str_id + "/picture?width="+PROFILE_PIC_SIZE+"&height="+PROFILE_PIC_SIZE);
                                    startActivity(home);


                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    Log.d("xxxx","aa");
                                }



                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();


            }

            @Override
            public void onCancel() {

                Log.v("LoginActivity", "cancel");
            }

            @Override
            public void onError(FacebookException exception) {

                Log.v("LoginActivity", exception.getCause().toString());

            }
        });
    }

    //After the signing we are calling this function
    private void handleSignInResult(GoogleSignInResult result) {
        //If the login succeed
        if (result.isSuccess()) {
            //Getting google account
            GoogleSignInAccount acct = result.getSignInAccount();
            //tv= (LinearLayout) tv.findViewById(R.id.layoutfragmentlogin);
            //tv.setVisibility(View.GONE); //hide include , so include now show nothing


            Intent Home=new Intent(this,HomeActivity.class);
            Home.putExtra("name",acct.getDisplayName());
            Home.putExtra("email", acct.getEmail());
            Home.putExtra("URL",acct.getPhotoUrl());
            startActivity(Home);

        } else {
            //If login fails
            Toast.makeText(this, "Login Failed on silentsign in", Toast.LENGTH_LONG).show();
        }
    }

    //@Override
    public void onConnected(Bundle bundle) {
        mSignInClicked = false;


        Toast.makeText(myContext, "User is connected!", Toast.LENGTH_LONG).show();

    }

   //@Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(callbackmanager!=null) {
            callbackmanager.onActivityResult(requestCode, resultCode, data);
            Log.d("ani", "onActivityResult:" + requestCode + ":" + resultCode + ":" + data);
        }

        if (requestCode == RC_SIGN_IN) {
            // If the error resolution was not successful we should not resolve further.
            if (resultCode != this.RESULT_OK) {
                mShouldResolve = false;
            }

            mIsResolving = false;
            mGoogleApiClient.connect();
        }
    }

    //@Override
    public void onConnectionFailed(ConnectionResult connectionResult)
    {
        //prefs.edit().putBoolean("Islogin",false).commit();
        //DO Nothing..


        /*
        //==========Below is Trying to connect if googleUser not connected already =======
        // Could not connect to Google Play Services.  The user needs to select an account,
        // grant permissions or resolve an error in order to sign in. Refer to the javadoc for
        // ConnectionResult to see possible error codes.
        Log.d("ani", "onConnectionFailed:" + connectionResult);

        if (!mIsResolving && mShouldResolve) {
            if (connectionResult.hasResolution()) {
                try {
                    connectionResult.startResolutionForResult(getActivity(), RC_SIGN_IN);
                    mIsResolving = true;
                } catch (IntentSender.SendIntentException e) {
                    Log.e("ani", "Could not resolve ConnectionResult.", e);
                    mIsResolving = false;
                    mGoogleApiClient.connect();
                }
            }
        }*/

    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to exit?")
                .setTitle("HnBS Alert")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        finishAffinity();

                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }





}

EDIT 2 : MY HOMEACTIVITY CODE

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}


/**
 * react to the user tapping/selecting an options menu item
 */
@Override

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_menu_logout:
            LoginManager.getInstance().logOut();  //LogOut from Facebook

            //logout from login manual
            sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putBoolean("IsLoggedManual",false).commit();

            //if (mGoogleApiClient.isConnected()) {
              //  if (mGoogleApiClient.isConnected())
              //      Auth.GoogleSignInApi.signOut(mGoogleApiClient);

            //}

            Toast.makeText(this, "LoggedOut!", Toast.LENGTH_SHORT).show();
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    finishAffinity();

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

}

3

Answers


  1. Chosen as BEST ANSWER

    After eight hours trial with some debugging, i couldn't find any trace of what is causing my HomeActivity fires twice. for people who stuck in any similar case, if you want and if its not breaking out your code, you can try to make your activity appear only one instance by adding this on your activity declaration in the manifest:

    android:launchMode = "singleTask"   
    

    this has been the solution for me right now cause i don't want to waste any time longer as i need to move on to the next progress. thank you for any assistance.


  2. You are calling the activity twice. That is
    when consider if you are logged in facebook and google then the both the code get executed.

    in onCreate you are calling

    if (opr.isDone())
                {
                    // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
                    // and the GoogleSignInResult will be available instantly.
                    Log.d("TAG", "Got cached sign-in");
    
                    GoogleSignInResult result = opr.get();
    
                    handleSignInResult(result); //check method for going to home activity
    
                }
    

    and

    if ( AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile()!=null ) {
                    //load profile and skip (loginfragment) to Home page
                    Intent home = new Intent(MainActivity.this , HomeActivity.class);
                    startActivity(home);
    
                } else if ( IsLoggedManual ) {   //IF already LOGGED IN MANUAL (SHAREDPREF)
                    Intent home = new Intent(MainActivity.this , HomeActivity.class);
                    startActivity(home);
    
                }
    

    Use these conditions as one eg

    opr.isDone() ||  AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile()!=null
    
    Login or Signup to reply.
  3. Here you missed it, check out this code –

    OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
            if (opr.isDone()) {
                // If the user's cached credentials are valid, the OptionalPendingResult will be "done"
                // and the GoogleSignInResult will be available instantly.
                Log.d("TAG", "Got cached sign-in");
    
                GoogleSignInResult result = opr.get();
    
                handleSignInResult(result);
            }
    

    Above cached sign-in will start HomeActivity as well as if your AccessToken is not null(you’re logged in with facebook) – again will start Home Activity

            //CHECK IF ALREADY LOGGED BY FB
            if ( AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile()!=null ) {
                //load profile and skip (loginfragment) to Home page
                Intent home = new Intent(MainActivity.this , HomeActivity.class);
                startActivity(home);
            }
    

    Solution:
    You should put these conditions using else if ladder statements…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search