活动正在对机器人产生两次 [英] Activity being created twice on android

查看:128
本文介绍了活动正在对机器人产生两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是新来的Andr​​oid开发,所以请宽容我。

First of all, I'm new to Android Development so please have patience with me.

我会从UI开始,我有,一旦你点击一个按钮,启动了结果的活动。

I'll start from the UI, I have a button that once you tap it, starts an activity for a result.

public class GUIActivity extends Activity

    @Override
    public void onClick(....){
        Intent intent = new Intent(getApplicationContext(), GetImageActivity.class);
        intent.putExtra("action", FROM_CAMERA);
        startActivityForResult(intent, GET_IMAGE);
    }

    @Override
    onActivityResult(int requestCode, int resultCode, Intent data){
        Log(TAG, "onActivityResult");
        //handle result
    }

}

GetImageActivity 类包装了两个活动,一个捕捉摄像机等图像从画廊得到它。它返回与所选图像的Uri对象。

The GetImageActivity class is a wrapper for two other activities, one to capture an image from the camera and other to get it from the gallery. It returns and Uri object of the selected image.

public class GetImageActivity extends Activity{
    private Uri mediaUri;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        int action =  extras.getInt("action");

        Log.d(TAG, "onCreate");

        switch(action){
            case FROM_CAMERA:
                mediaUri = Uri.fromFile(new File(....));
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, mediaUri);

                Log.d(TAG, "Calling camera activity"
                startActivityForResult(intent, action);
                break;
            case FROM GALLERY:
                //...
        }            
    }

    @Override
    onActivityResult(int requestCode, int resultCode, Intent data){
        Log.d(TAG, "onActivityResult");

        switch(requestCode){
            case FROM_CAMERA:
                if(resultCode == Activity.RESULT_OK){
                    Intent data = new Intent();
                    data.putExtra("uri", mediaUri);
                    setResult(Activity.RESULT_OK, data);
                    finish();
                }else{
                    Log.e(TAG, "Camera activity failed!");
                    setResult(Activity.RESULT_CANCELED);
                    finish();
                }
                break;

            case FROM_GALLERY:
                //...

        }
    }
}

这是预期,当用户点击该按钮要发生:

This is what is expected to happen when the user clicks on the button:

  • 在相机的活动应该开始。
  • 一旦用户拍照就应该发回它的URI的GUI类。

有时候(它通常是一个50%的机会),它工作于预期,但其他时间,这是发生了什么:

Sometimes (it's usually a 50% chance) it works at expected, but other times this is what happens:

  • 相机活动开始。
  • 在用户需要的图片。
  • 相机活动重新开始。
  • 的用户可以拍摄另一张照片或回去。
  • 在两种情况下,URI,获取回GUI类并不存在。

我添加了一对夫妇的调试日志行按照事件发生的顺序。当我得到的不良行为,这是我得到的输出:

I've added a couple of debug log lines to follow the sequence of events. When I get the bad behaviour this is the output I get:

  • GetImageActivity - 的onCreate
  • GetImageActivity - 调用摄像头活动

相机打开,一旦我拍摄的照片,它说:

The camera opens, and once I've taken a picture it says:

  • GetImageActivity - 的onCreate(再)
  • GetImageActivity - 调用摄像头活动
  • GetImageActivity - onActivityResult

相机开启第二次。用户需要另一张照片和:

The camera opens for the second time. The user takes another picture and:

  • GetImageActivity - onActivityResult
  • GUIActivity - onActivityResult

所以我的问题是,有什么能引起 GetImageActivity 被调用两次?

So my question is... what could cause the GetImageActivity to be called twice?

推荐答案

现在的问题是处理不当的活动的的生命周期。

The problem is improper handling of the Activity lifecycle.

第二次调用的onCreate 的处理结果。

The second call to onCreate is for handling the result.

Android的可以选择摧毁活动正在等待调用 onActivityResult ;尤其是当免费的内存不足。有些设备显得更加积极地摧毁Activitys是对任务堆栈。我能够可靠三星设备设置为调试模式称为严格模式。

Android may choose to destroy an Activity that is waiting for the call to onActivityResult; especially when free memory is running low. Some devices appear more aggressive about destroying Activitys that are on the task stack. I can reliably recreate the issue on a Samsung device set to a debugging mode called "strict mode".

您可以验证这是否是你的问题,通过登录调用的onCreate &放大器; 的onDestroy

You can verify whether this is your issue by logging calls to onCreate & onDestroy.

在一个被毁坏的活动,该活动的结果需要处理时的情况下,Android将会重新创建活动,传递一个 savedInstanceState 的onCreate 。因此,补救方法是检查 savedInstanceState 在你的 GetImageActivity.onCreate 的值。如果不是然后不要做出 startActivity ,因为你的活动的任何来电正在重新调用 onActivityResult

In the case of a destroyed activity, when the activity result needs to be processed, Android will recreate the Activity, passing a savedInstanceState to onCreate. So, the remedy is to check the value of savedInstanceState in your GetImageActivity.onCreate. If it is not null then don't make any calls to startActivity because your Activity is being recreated to call onActivityResult.

另外,如果你需要preserve任何状态,那么将覆盖的onSaveInstanceState(包outState),把你需要的数据放入 outState

Optionally, if you need to preserve any state then override onSaveInstanceState(Bundle outState) and put data you need into outState.

这篇关于活动正在对机器人产生两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆