简体   繁体   English

在onActivityResult之后,屏幕旋转为横向

[英]After onActivityResult the screen rotate to landscape

I'm tring to open camera and take a picture and after that to store the image in a listView with image. 我正准备打开相机拍照,然后将图像存储在带有图像的listView中。 the problem is, after i take the picture and return to my activity result the screen returns in landscape and a short while after rotates back to portrait and make me to lost the data. 问题是,在我拍照并返回活动结果后,屏幕以横向显示,然后旋转回纵向不久,使我丢失了数据。 why this is happened? 为什么会这样呢? here is my code: 这是我的代码:

  if (requestCode == mActions.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bitmap imageCapturred = (Bitmap) data.getExtras().get("data");
        list.add(new ModelChat(null , false,null ,imageCapturred));
        mChatAdapter = new ChatRowAdapter(this , list);
        mChatList.setAdapter(mChatAdapter);
        System.out.println("Get Image from Camera");
    }

but the way.. I dont want using android:screenOrientation="portrait" Any idea how to slove it? 但方式..我不想使用android:screenOrientation="portrait"任何想法如何解决它?

make a intent as class variable and set the value of this intent when onActivityResult called like this.camaraData =data; 将intent作为类变量,并在这样调用onActivityResult时设置该intent的值。camaraData = data; and also save this class variable in onsavedInstanceState() function and restore this value in onRestoreInstanceState() or in oncreate() eg. 并将此类变量保存在onsavedInstanceState()函数中,并在onRestoreInstanceState()oncreate()还原此值。

            public class yourActivityName extends Activity
            {

            Intent camaraData ;boolean chkCalled;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);   
                // Check whether we're recreating a previously destroyed instance
                if (savedInstanceState != null) {
                    // Restore value of members from saved state
              camaraData =savedInstanceState.getParcelable("camaraData");   
             if(!chkCalled){
    onActivityResult(mActions.REQUEST_IMAGE_CAPTURE,Activity.Result_OK,camaraData); 
    chkCalled=true;
        }
    }  
            ...... /* your code goes here */  ...
    }


            @Override
            public void onSaveInstanceState(Bundle savedInstanceState) {
                // Save the user's current game state
                savedInstanceState..putParcelable("camaraData", camaraData);    
            // Always call the superclass so it can save the view hierarchy state
                super.onSaveInstanceState(savedInstanceState);
            }
            @Override
            public void onActivityResult(int requestCode,int resultCode,Intent data) 
            {
               camaraData=data;
            if (requestCode==mActions.REQUEST_IMAGE_CAPTURE&&resultCode==RESULT_OK){
                    Bitmap imageCapturred = (Bitmap) data.getExtras().get("data");
                    list.add(new ModelChat(null , false,null ,imageCapturred));
                    mChatAdapter = new ChatRowAdapter(this , list);
                    mChatList.setAdapter(mChatAdapter);
                    System.out.println("Get Image from Camera");
                }
            }

            public void onRestoreInstanceState(Bundle savedInstanceState) {
                // Always call the superclass so it can restore the view hierarchy
                super.onRestoreInstanceState(savedInstanceState);       
                // Restore state members from saved instance
                camaraData = savedInstanceState.getParcelable("camaraData");     
        if(!chkCalled){ 
               onActivityResult(mActions.REQUEST_IMAGE_CAPTURE,
Activity.Result_OK,camaraData); 
chkCalled=true;
        }
            }

        }

call only one way to handle this either in onCreate() or by using onRestoreInstanceState() function your problem will me solve as per your requirement or you can keep both but call of onActivityResult(...) function only from one place by checking whether its called or not by using boolean value as given in example 仅在onCreate()或使用onRestoreInstanceState()函数中调用一种方法来解决此问题,您的问题将按照您的要求解决,或者您可以保留两者,但只能通过检查是否仅从一个位置调用onActivityResult(...)函数通过使用示例中给出的boolean值来调用或不调用它

In general, what you need to do if you want your activity to survive screen rotation is save/restore your activites instance state. 通常,如果希望活动在屏幕旋转后继续存在,您需要做的就是保存/恢复Activites实例状态。 Have a look at Recreating an Activity 看一下重新创建活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM