4

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

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]

已编辑

主要活动

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);
        }
    }
4

1 回答 1

1

startActivityForResult(login, AuthorizationManager.REQUEST_LOGIN);onresume()您的主要活动中删除。这样做,您将能够回到主要活动。onActivityResult如果您不想在完成后返回活动,也应该将其从您身上移除。

于 2013-08-14T15:42:38.370 回答