当我从另一个窗口返回时,前一个屏幕关闭(调用onDestroy)

时间:2012-12-07 12:13:11

标签: java android android-intent

我有一个主屏幕和登录屏幕。在主屏幕中,我调用登录屏幕。

问题是当我在登录并返回主页时。该应用程序调用onDestroy of Home并关闭。

以下是代码:

public class Home extends Activity {

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

        final ImageView login_username_btn = (ImageView) findViewById(R.id.login_username_btn);

        login_username_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Login.class);
                startActivity(intent);
            }
        });
    }

}


public class Login extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.login);

        final EditText etUsuario = (EditText) findViewById(R.id.txtUsuario);
        final EditText etSenha = (EditText) findViewById(R.id.txtSenha);
        final ImageView ivVoltar = (ImageView) findViewById(R.id.voltarBtn);

        ivVoltar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(findViewById(android.R.id.content)
                    .getWindowToken(), 0);

                finish();
            }
        });


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

        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(findViewById(android.R.id.content)
            .getWindowToken(), 0);
    }
}


And on LogCat shows "GC_EXTERNAL_ALLOC freed" and "GC_EXPLICIT freed" messages.

1 个答案:

答案 0 :(得分:0)

要返回主页,您正在调用finish();,这会破坏活动(完成活动已经在onClose()之后)。

enter image description here

而不是它创建HomeActivity的意图。这解决了这个问题。

startActivity(new Intent(getBaseContext(), HomeActivity.class);

或者

@Override
public void onBackPressed() 
{
    super.onBackPressed();
}

ivVoltar.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(findViewById(android.R.id.content)
                .getWindowToken(), 0);

            onBackPressed();
        }
    });