0

ここに奇妙なシナリオがあります。

私はこのコードを持っています:

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

setContentView(R.layout.main);

// ...next lines of code

また、first_time_check()関数は、ユーザーが初めてログインしたかどうかを確認します。ユーザー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 に答える