我的应用程序在屏幕旋转时丢失了一些数据

时间:2013-10-10 05:58:03

标签: android android-actionbar screen-rotation data-loss

我的应用程序在屏幕旋转时丢失了一些数据,因此我无法动态显示操作栏项目。

实际上在屏幕旋转中,第一次调用onCreate()然后调用OnSuccess()OnCreateOptionsMenu()并正确显示操作栏项目。但是下一次轮换它在OnCreateOptionsMmenu()之前调用了OnSuccess()

我在OnSuccess()中定义了OnCreateOptionsMenu()中使用的变量,因此我无法动态显示actionbarItem。

我已经定义了onRestoreInstanceState()onSaveInstanceState(),但这些内容的调用时间晚于OnCreateOptionMenu()

我已将ActionbarSherlock用于此项目。如果有人能够提供线索,那将是非常感激的。

这是我的代码

 public class EventDisplayActivity extends BaseActivity {

    private ZEvent zEventObject;

    private String mLastUpdatedUserID;
    private String mUpdateFlag;

    private ZEvent mEventData;
    private ZVenue mVenueData;
    private ZAddress mAddressData;
    private ZUpdates mUpdateData;
    private ZRiders mRiderData;

    MenuItem decline_item;
    MenuItem edit_item ;
    MenuItem accept_item;
    MenuItem rider_item;
    MenuItem updates_item; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_display);
        setTitle("Event View");   

        vip = new ViewInfoProcessor();

        //set UpNavigation      ;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getSupportMenuInflater().inflate(R.menu.event_display, menu);

        decline_item = menu.findItem(R.id.decline_item); // for decline
        edit_item = menu.findItem(R.id.edit_item); // for edit
        accept_item = menu.findItem(R.id.accept_item); // Accept not visible
        rider_item = menu.findItem(R.id.view_artist_rider); // view artist rider
        updates_item = menu.findItem(R.id.view_all_updates); // view all updates

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int itemId =  item.getItemId();

        if(itemId == R.id.edit_item){

            Bundle b = new Bundle();
            Intent intent = new Intent(this, EventEditActivity.class);
            intent.putExtra("usage_info","event_view");
            intent.putExtra("request_from","event_view");
            intent.putExtras(b);

            intent.putExtra("EVENT_PARCEL", mEventData);
            intent.putExtra("VENUE_PARCEL", mVenueData);
            intent.putExtra("ADDRESS_PARCEL", mAddressData);
            startActivity(intent);

            return true;

        }
            return false; //return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {        


        if(mEventStatus.equals("Inquiry")){

            boolean expired = vip.isExpiredDecider(mEventDate);
            if(expired){                
                decline_item.setVisible(false); // for decline
                edit_item.setVisible(false); // for edit
                accept_item.setVisible(false); // Accept not visible
                rider_item.setVisible(false); // view artist rider
                updates_item.setVisible(false); // view all updates
            }else{
                decline_item.setVisible(true); // for decline
                edit_item.setVisible(true); // for edit

                accept_item.setVisible(false);

                if (mRiderId != null) {
                    rider_item.setVisible(true); // view artist rider
                } else {
                    rider_item.setVisible(false);  // view artist rider
                } 

                Log.i(">>>>","flag"+ mUpdateFlag);
    //here I used the mUpdateFlag varible value

               if(mUpdateFlag != null){

                   if(mUpdateFlag.equals("1") && (!mLastUpdatedUserID.equals(mLoggedUserId))){
                        updates_item.setVisible(true); // view all updates
                    }else{
                        updates_item.setVisible(false);  // Not view all updates
                    }
               }else{
                   updates_item.setVisible(false); //view all updates
               }

            }           
        }// end 



        return super.onPrepareOptionsMenu(menu);

    };

    AsyncHttpResponseHandler eventResponse = new AsyncHttpResponseHandler() {
        public void onSuccess(int arg0, String arg1) {
            System.out.println("onSuccess() in Event Dispaly" );
            try {


                JSONArray proposalArray = new JSONObject(arg1).getJSONArray("proposal");                
                JSONArray riderArray = new JSONObject(arg1).getJSONArray("rider");
                JSONArray updatesArray = new JSONObject(arg1).getJSONArray("updates");
                JSONArray agreementArray = new JSONObject(arg1).getJSONArray("agreement_details");
                JSONArray ratingsArray = new JSONObject(arg1).getJSONArray("rating_details");

                mEventData = new ZEvent(proposalArray.getJSONObject(0));    
                mVenueData = new ZVenue(proposalArray.getJSONObject(0));
                mAddressData = new ZAddress(proposalArray.getJSONObject(0));

                if(!updatesArray.isNull(0)){
                    mUpdateData =  new ZUpdates(updatesArray.getJSONObject(0));
                }

                if(!riderArray.isNull(0)){
                    mRiderData = new ZRiders(riderArray.getJSONObject(0)); 

                }               


                if(mRiderData != null){
                    mRiderId = mRiderData.getRiderID();
                    String mRiderText = mRiderData.getRiderText();
                    String mRiderStatus = mRiderData.getRiderStatus();
                }

                if(mUpdateData != null){
                    mUpdateFlag = mUpdateData.getUpdateFlag();
                    Log.i("define 1st time:::","flag"+mUpdateFlag);                   
                    mLastUpdatedUserID = mUpdateData.getLastUpdatedUserID();
                }   


                defineViews(mEventData, mVenueData, mAddressData, mRiderData, mUpdateData);

            } catch (JSONException e) {
                e.printStackTrace();
            }

        };
        public void onFailure(Throwable arg0) {

        };      
    };




    @Override
    protected void onInit(ModelBase... data) {
        System.out.println("onInit() called in EDA");

        SharedPreferences sessionkey = getApplicationContext().getSharedPreferences("session_detail", Context.MODE_PRIVATE);        
        session_token = sessionkey.getString("session", "");
        mLoggedUserType = sessionkey.getString("user_type", ""); 
        mLoggedUserId = sessionkey.getString("user_id", "");

        Intent i = getIntent();
        Bundle extras = i.getExtras();
        zEventObject = extras.getParcelable("EVENT_ITEM"); 

        mProposalId = zEventObject.getProposalID();
        mEventStatus = zEventObject.getProposalStatus();
        mEventDate = zEventObject.getEventDate();
        mAgreedPromoterUserId = zEventObject.getAgreedPromoterUserID();
        mAgreedBAUserId = zEventObject.getAgreedAgentUserID();
        zEventObject.getLastUpdatedByUserID();

        params = new RequestParams();
        params.put("token", session_token);
        params.put("proposal_id", mProposalId);

        Log.i("PRPOPOSAL ID",""+mProposalId +""+ session_token);
        mApiClient.getView(eventViewURL, params, eventResponse);

    }

    private void defineViews(ZEvent eItem, ZVenue vItem, ZAddress aItem, ZRiders rItem, ZUpdates uItem){


        // here goes UIs
        } 

    }// End DefineView

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        outState.putParcelable("EVENT_ITEM", zEventObject);
        outState.putParcelable("UPDATE_ITEM", mUpdateData);

        System.out.println("onSaveInstanceState()");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);
        zEventObject = savedInstanceState.getParcelable("EVENT_ITEM");
        mUpdateData = savedInstanceState.getParcelable("UPDATE_ITEM");

        if(mUpdateData != null){
            mUpdateFlag = mUpdateData.getUpdateFlag();
            Log.i("define 2st time:::","flag"+mUpdateFlag);                   
            mLastUpdatedUserID = mUpdateData.getLastUpdatedUserID();
        }

        System.out.println("onRestoreInstanceState()");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        super.onConfigurationChanged(newConfig);
        setContentView(R.layout.activity_event_display);

    }

}
问题仍然存在!

实际上在屏幕旋转中,第一次调用onCreate()然后调用OnSuccess()OnCreateOptionsMenu()并正确显示操作栏项目。但是下一次轮换它在OnCreateOptionsMmenu()之前调用了OnSuccess()

2 个答案:

答案 0 :(得分:0)

您是否尝试过清单中的以下代码..

 <activity
      android:name=".YourActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>

答案 1 :(得分:0)

当您的屏幕旋转以便oncreate方法再次调用stooping时,我们使用它 清单文件中的android:configChanges =“orientation | screenSize”。