0

我这里有一个奇怪的场景。

我有这个代码:

// For checking if the person is logged in.
first_time_check();

setContentView(R.layout.main);

// ...next lines of code

first_time_check() 函数检查用户是否是第一次登录。如果他们的 user_id 不在 SharedPreferences 中,我会将他们重定向到登录:

public void first_time_check()
{   
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( ProblemioActivity.this);

    String user_id = prefs.getString( "user_id", null ); // First arg is name and second is if not found.
    String first_time_cookie = prefs.getString( "first_time_cookie" , null );               

    // About setting cookie.  Check for first time cookie
    if ( first_time_cookie == null )
    {
        // This user is a first-time visitor, 1) Create a cookie for them
        first_time_cookie = "1";

        // 2) Set it in the application session.
        prefs.edit().putString("first_time_cookie", first_time_cookie ).commit(); 

        // Make a remote call to the database to increment downloads number
        // AND send me an email that it was a new user.                 
    }        
    else
    {
        // If not first time, get their id.
        // If user_id is empty, make them an account.  
        // If id not empty, call the update login date and be done.         

        if ( user_id == null )
        {
            // User id is null so they must have logged out.                                
            Intent myIntent = new Intent(ProblemioActivity.this, LoginActivity.class);
            ProblemioActivity.this.startActivity(myIntent);
        }
        else
        {               
            // Make a remote call to the database to increment downloads number                                 
        }
    }

return;
}       

所以在代码执行之后

            Intent myIntent = new Intent(ProblemioActivity.this, LoginActivity.class);
            ProblemioActivity.this.startActivity(myIntent);

它仍然在调用此函数的原始代码下方执行。

知道怎么会发生吗?

谢谢!!

4

1 回答 1

1

这摘自开发指南

Shutting Down an Activity
You can shut down an activity by calling its finish() method.
You can also shut down a separate activity that you previously
started by calling finishActivity().

Note: In most cases, you should not explicitly finish an activity
using these methods. As discussed in the following section about the
activity lifecycle, the Android system manages the life of an
activity for you, so you do not need to finish your own activities.
Calling these methods could adversely affect the expected user
experience and should only be used when you absolutely do not want
the user to return to this instance of the activity.

在活动上调用 finish() 在这里似乎是合适的,因为您不希望用户返回此活动。

于 2012-05-24T02:43:52.950 回答