Why is Activity instantiated twice?

Asked

Viewed 307 times

0

I am implementing the facebook sdk to log in to my application. I have two activities that I call Login and Home. When I click the login button, the login is performed perfectly and the Activity Home is instantiated as soon as the onSessionStateChange method is called:

private void onSessionStateChange(Session session, SessionState state,
        Exception exception) {
    if (state.isOpened()) {
        Intent intent = new Intent(getActivity().getBaseContext(), HomeActivity.class);
        startActivity(intent);
        getActivity().finish();
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }

}

The problem is that the code instance twice to Activity Home.

  • Try to override the onBackPressed at Activity Home. Check that you are logged in and only finish Activity Home without calling super method (there should be a better solution).

  • I did it myself, but like you said, I don’t think it’s the best solution.

  • You have to finalize the acitivity of the new Activity that you are calling, no?

  • Yes. When you press the back button of the device this is exactly what happens.

  • I mean, pass the Bundle of intent the reference to Activity login and, when starting the other Activity, ends the login Activity.

  • I edited the question

  • This issue changes a lot. If you have two instances of Homeactivity you will have to show more code because the problem is definitely not there.

Show 2 more comments

1 answer

0


There’s a great chance the callback onSessionStateChange is being called more than once with the same state.

There are scenarios where this can happen such as the point at which you are making your initial call.

If it is in the onResume try to change it to stay in onCreate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.