Android on finish()重新创建Activity

时间:2013-08-14 15:09:31

标签: android android-activity

如果我尝试完成活动,就会发生奇怪的事情。在第一次调用活动完成方法后 - 重新创建活动。

BroadcastReceiver requestReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                long resultRequestId = intent.getLongExtra(DVSServiceHelper.EXTRA_REQUEST_ID, 0);

                if(BuildConfig.DEBUG) Log.d(TAG, "received request id: "+ resultRequestId +", current request id: "+ requestId);

                if (resultRequestId == requestId) {
                    int resultCode = intent.getIntExtra(DVSServiceHelper.EXTRA_RESULT_CODE, 0);

                    Logger.debug(TAG, "result code: "+ resultCode);

                    switch(resultCode) {
                        case HttpsURLConnection.HTTP_OK:
                            //Dummy intent
                            Intent in = new Intent();

                            Logger.debug(TAG, "HTTP OK");

                            //User logged in successfuly. Finish Login activity
                            LoginActivity.this.setResult(AuthorizationManager.LOGIN_SUCCESS, in);
                            Logger.debug(TAG, "setting result...");
                            LoginActivity.this.finish();
                            Logger.debug(TAG, "finishing...");

                            break;
                            ...

活动onCreate方法:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        Logger.debug(TAG, "[onCreate]");
        ...

LogCat输出

第一次尝试

08-14 17:46:00.917: D/(7975): [LoginActivity]        result code: 200
08-14 17:46:00.917: D/(7975): [LoginActivity]        HTTP OK
08-14 17:46:00.917: D/(7975): [LoginActivity]        setting result...
08-14 17:46:00.937: D/(7975): [LoginActivity]        finishing...
08-14 17:46:00.977: D/(7975): [LoginActivity]        [onCreate]

秒尝试:

08-14 17:50:25.437: D/(7975): [LoginActivity]        result code: 200
08-14 17:50:25.437: D/(7975): [LoginActivity]        HTTP OK
08-14 17:50:25.437: D/(7975): [LoginActivity]        setting result...
08-14 17:50:25.537: D/(7975): [LoginActivity]        finishing...
08-14 17:50:25.567: D/(7975): [MainActivity]         [onActivityResult]
08-14 17:50:25.567: D/(7975): [MainActivity]         [onActivityResult]
08-14 17:50:25.567: D/(7975): [MainActivity]         [onResume]

EDITED

MainActivty

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Logger.debug(TAG, "[onActivityResult]");

        if(AuthorizationManager.REQUEST_LOGIN == requestCode) {
            if(AuthorizationManager.LOGIN_FAILED == resultCode) {

                Logger.debug(TAG, "Login failed!");

                Intent login = new Intent(DVSApplication.getContext(), LoginActivity.class);
                startActivityForResult(login, AuthorizationManager.REQUEST_LOGIN);
            }
        }
    }



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

        Logger.debug(TAG, "[onCreate]");

        //if user isn't saved crediantials - show log in activity
        if(!AuthorizationManager.getManager(getApplicationContext()).isLoggedIn()) {

            Logger.debug(TAG, "[onCreate] user is not logged in!");

            Intent login = new Intent(DVSApplication.getContext(), LoginActivity.class);
            startActivityForResult(login, AuthorizationManager.REQUEST_LOGIN);
        }
        ...
}

protected void onResume() {
        super.onResume();

        Logger.debug(TAG, "[onResume]");

        //if user isn't saved crediantials - show log in activity
        if(!AuthorizationManager.getManager(getApplicationContext()).isLoggedIn()) {

            Logger.debug(TAG, "[onResume] User is not logged in!");

            Intent login = new Intent(DVSApplication.getContext(), LoginActivity.class);
            startActivityForResult(login, AuthorizationManager.REQUEST_LOGIN);
        }
    }

1 个答案:

答案 0 :(得分:1)

从主要活动的startActivityForResult(login, AuthorizationManager.REQUEST_LOGIN);中删除onresume()。这样做,您将能够回到主要活动。如果你不想在活动结束后再回到活动,你也应该从你onActivityResult删除它。