katit
katit

Reputation: 17915

Activity lifecycle - some strange behavior. Can't duplicate

That might be broad question but I will try to explain best. I will also give code snippets if anything jumps out - please let me know.

I can't repro this issue but it surface itself like so:

a. Sometime I would be logged in - I hit log out and will see my Activity refreshed. And that can happen many times. What I expect is Activity to close when I hit logout.

b. Sometime I'm on my Activity (already logged in) and started from icon. And I click back and it wouldn't go to "desktop" and goes to activity again. And this can happen 1-3 times. I expect since this is main activity to go back to desktop when hit back.

c. I noticed similar kind of issue on Yahoo mail app so I'm not sure if that is bug or 2.3.1 issue on my Nexus S?

I have: 1. Main activity 2. Login dialog that is custom dialog.

This is code from my activity:

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //If not authorized yet then show sign in dialog
        if (!PreferencesManager.getIsUserAuthenticated(getApplicationContext()))
        {
            showDialog(1);
        }

        Button signOutButton = (Button) findViewById(R.id.SignOutButton);
        signOutButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View view)
            {
                PreferencesManager.setIsUserAuthenticated(false, getApplicationContext());
                **MainActivity.this.finish();**
            }
        });


    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();

    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    protected Dialog onCreateDialog(int id)
    {
        LoginDialog dialog = new LoginDialog(this);

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener()
        {
            public void onDismiss(DialogInterface dialog)
            {
                if (!PreferencesManager.getIsUserAuthenticated(getApplicationContext()))
                {
                    **MainActivity.this.finish();**
                }
            }
        });

        return dialog;
    }

In dialog class - I have regular stuff and "this.dismiss" if Login was successfull.

Upvotes: 0

Views: 156

Answers (2)

JQCorreia
JQCorreia

Reputation: 717

I'm not sure this is the correct way to close an Activity, but one thing you can do is to change the launchMode in your Manifest to singleTop to prevent the start of various instances of that Activity.

Hope it helps, JQCorreia

Upvotes: 1

Mike dg
Mike dg

Reputation: 4658

Your activity is somehow getting created multiple times. This can be do to how it's being run, if it's a shortcut from your desktop or the app drawer, or holding home, or some other shortcut.

You might want to look into launchMode at http://developer.android.com/guide/topics/manifest/activity-element.html

Upvotes: 0

Related Questions