Android how to save realm results with onSaveInstanceState for screen rotation

时间秒杀一切 提交于 2019-12-11 06:36:46

问题


My problem is this: When i open dialog fragment, i send realm and realm results from activity to dialog fragment. I always send different realm results to dialog fragment, depending what i click in activity. This is code in dialog fragment where i receive realm and realmResults from activity, when i click to open dialog fragment:

  public void setChangeNoteListener(ChangeNoteListener mChangeNoteListener, RealmResults<Drop> realmResults, Realm realm) {
        mNotelistener = mChangeNoteListener;
        mRealmResolts = realmResults;
        mRealm = realm;
    }

After i receive them, i use them for showing some data in dialog fragment.

Problem is when i change app orientation, app get crushed because i can not use realm results anymore. My question is, how to save the whole realm results with onSaveInstanceStat and when app orientation change, to use that realm results again.

Note that in realm results is not only one item, there is more items.

EDIT: I save date from realm with

 @Override
    public void onSaveInstanceState(Bundle outState) {
        Bundle argumetns = getArguments();
        int position = argumetns.getInt("POSITION");
        outState.putString("myTitle", String.valueOf(mRealmResolts.get(position).getWhat()));
        outState.putString("myNote", String.valueOf(mRealmResolts.get(position).getWhat_note()));
        super.onSaveInstanceState(outState);
    }

When i load app i load app like this:

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mBtnClose = (ImageButton) view.findViewById(R.id.btn_close_note);
        mBtnDelete = (ImageButton) view.findViewById(R.id.dialog_btn_delete_note);
        mEditTextTitle = (EditText) view.findViewById(R.id.dialog_text_naslov);
        mEditTextNote = (EditText) view.findViewById(R.id.dialog_text_note);
        mBtnEdit = (Button) view.findViewById(R.id.dialog_btn_edit);
        mBtnCompleted = (Button) view.findViewById(R.id.dialog_btn_completed);
        mBtnSave = (Button) view.findViewById(R.id.dialog_btn_save);
        mBtnForward = (ImageButton) view.findViewById(R.id.dialog_btn_forward);
        mBucketNoteTime = (BucketPickerView) view.findViewById(R.id.bpv_date_in_note);
        mBucketNoteTimeNonSelected = (BucketPickerView_Unselected) view.findViewById(R.id.bpv_date_in_note_unselected);
        mSwitchButton = (Switch) view.findViewById(R.id.switchButtonNote);
        mSwitchChangeDate = (Switch) view.findViewById(R.id.switchChangeDate);
        mSwitchNoteText = (TextView) view.findViewById(R.id.switchTextAbboveNote);
        dialogPickerButtonNote = (ImageView) view.findViewById(R.id.dialog_picker_button_note);


        mBtnClose.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
        mBtnEdit.setOnClickListener(this);
        mBtnCompleted.setOnClickListener(this);
        mBtnSave.setOnClickListener(this);
        mBtnForward.setOnClickListener(this);
        dialogPickerButtonNote.setOnClickListener(this);

        if (savedInstanceState != null) {
            Log.d("realmtest", "onViewCreated: prvi");
            String mojStringTitle = savedInstanceState.getString("myTitle");
            String mojStringNote = savedInstanceState.getString("myNote");
            mEditTextTitle.setText(mojStringTitle);
            mEditTextNote.setText(mojStringNote);
            buttonWhenSaveVisibility();
            setTitleNoteTextToDialogNote();
        }
        else {
            buttonWhenSaveVisibility();

            setTitleNoteTextToDialogNote();
        }
  }

App get crushed when i rotate screan and and call method setTitleNoteTextToDialogNote() on this line of code (when i try to get data from realm).

        mEditTextTitle.setText(mRealmResolts.get(position).getWhat());

Here is error:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'io.realm.RealmModel io.realm.RealmResults.get(int)' on a null object reference
                      at com.petar.android.simplenote.DialogNote.setTitleNoteTextToDialogNote(DialogNote.java:231)
                      at com.petar.android.simplenote.DialogNote.onViewCreated(DialogNote.java:134)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1272)
                      at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2149)
                      at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:600)
                      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                      at com.petar.android.simplenote.ActivityMain.onStart(ActivityMain.java:296)
                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                      at android.app.Activity.performStart(Activity.java:6696)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4519) 
                      at android.app.ActivityThread.-wrap19(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

回答1:


RealmResults<T> represents the query results you obtain from the Realm instance.

Realm is a database. It stores things. It's a persistence solution.

If there is something in the Realm, then that means you don't have to persist it in onSaveInstanceState(), because it's already in the Realm file.

You should just recreate the RealmResults<T> with the same sort parameter and you should be fine.

Also, seek your bug in whatever calls setChangeNoteListener and doesn't initialize the RealmResults.



来源:https://stackoverflow.com/questions/41758625/android-how-to-save-realm-results-with-onsaveinstancestate-for-screen-rotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!