Android - 主要活动无缘无故结束?

时间:2015-03-18 20:38:47

标签: android android-intent

在主要活动中,我会通过阅读SharedPreferences来检查用户是否第一次登录。然后我开始一个新的简单的Login / SignUp活动。

按下"登录"第二个活动上的按钮,我想返回用户最后一次登录主活动(我使用getIntent().putExtra())。

出于某种原因,当我按下"登录",第二个活动关闭,然后我看到一个Toast有上次登录时间,但应用程序立即关闭!这就像正在关闭主要活动。我不知道为什么......以下是代码:

主要活动:

package com.technion.saharav.technionmobilehomework13_3_15;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends Activity {

    private static final int REQ_CODE_LOGIN = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
        startActivityForResult(intent, REQ_CODE_LOGIN);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQ_CODE_LOGIN:
                if (resultCode == LoginActivity.RESULT_FAIL) {
                    finish();
                }

                if (resultCode == RESULT_OK) {
                    Toast.makeText(getApplicationContext(),
                            "Last time connected: " + data.getExtras().get("lastLoginTime").toString(),
                            Toast.LENGTH_LONG).show();
                }

                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

LoginActivity:

package com.technion.saharav.technionmobilehomework13_3_15;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Date;


public class LoginActivity extends Activity {

    protected static int RESULT_FAIL = -1;

    EditText editext_username, editext_password;
    View tv;
    Button bt;
    private String username, password;

    Intent intent;
    SharedPreferences pref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editext_username = (EditText) findViewById(R.id.editText_Username);
        editext_password = (EditText) findViewById(R.id.editText_Password);
        tv = findViewById(R.id.textView_loginBottom);
        bt = (Button) findViewById(R.id.buttonClick);

        intent = getIntent();
        pref = getApplicationContext().getSharedPreferences("LoginInfo", MODE_PRIVATE);
        editor = pref.edit();

        if (!pref.getBoolean("firstLogin", true)) {
            username = pref.getString("username", "");
            password = pref.getString("password", "");
            tv.setVisibility(View.GONE);
        }
        else {
            bt.setText("CREATE");
        }


    }

    public void buttonClick(View view) {
        String userInput = editext_username.getText().toString();
        String passwordInput = editext_password.getText().toString();

        switch (bt.getText().toString()) {

            case "LOGIN":
                if (editext_username.getText().toString().equals(username)
                        && editext_password.getText().toString().equals(password)) {

                    intent.putExtra("lastLoginTime", pref.getString("lastLoginTime", ""));

                    editor.putString("lastLoginTime", Calendar.getInstance().getTime().toString());
                    editor.commit();

                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Login failed. Check the username or password and try again.", Toast.LENGTH_LONG).show();
                }
                break;

            case "CREATE":
                if (userInput.length() > 0 && passwordInput.length() > 0) {
                    editor.putString("username", userInput);
                    editor.putString("password", passwordInput);
                    editor.putString("lastLoginTime", Calendar.getInstance().getTime().toString());
                    editor.putBoolean("firstLogin", false);
                    editor.commit();
                    finish();
                }
                else {
                    Toast.makeText(getApplicationContext(),
                            "Username/Password cannot remain empty!", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_FAIL, intent);
        super.onBackPressed();
    }
}

1 个答案:

答案 0 :(得分:1)

您定义了protected static int RESULT_FAIL = -1;

...与RESULT_OK的值相同。

因此,当onActivityResult()运行时,条件resultCode == LoginActivity.RESULT_FAIL为真,因此执行finish()。请注意,finish()只是告诉系统结束您的活动;它不会弹出像return这样的调用堆栈,因此继续执行。

然后因为您编码if而不是else if,下一个条件resultCode == RESULT_OK 评估为true,因此会显示您的Toast。

然后因为您调用finish()您的活动退出。

在定义结果代码时,可以使用Activity.RESULT_FIRST_USER作为起始值;它被定义为1。

最好的方法是做这样的事情:

public static final int RESULT_FAIL = RESULT_FIRST_USER;
public static final int RESULT_OTHER = RESULT_FIRST_USER + 1;
public static final int RESULT_CRASH = RESULT_FIRST_USER + 2;

等。等

我还在结果代码中使用嵌套的switch语句,在请求代码外部。然后我知道只有一个块将要执行...除非我忘记break语句。 :)