techDigi
techDigi

Reputation: 251

Save custom object on screen rotate in Fragment or Activity

I know this question is very common and I have read so many different answers but none fits in my problem. In my application, I have an activity and in rhye activity I load a fragment. I also send some data(in the form of Bundle) to the fragment. So my Problem is when the screen is rotated, I save the fragment in onSaveInstanceState Activity method and check in onCreate Method weather savedInstance is null or not and on that basis I load the fragment. Activity code :

 @Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putParcelable(Const.TAG_REQ_CUSTOM,DetailsItems);
    outState.putString(Const.TAG_FLOW, Const.TAG_MAIN_FLOW);
    getSupportFragmentManager().putFragment(outState,"current_fragment",fragment);
}

onCreate Method :

if (findViewById(R.id.fragment_frame) != null) {
        if (savedInstanceState != null) {
// this invoke when screen rotate but the app crash 

            DetailsItems = savedInstanceState.getParcelable(Const.TAG_REQ_CUSTOM);
            String flow = savedInstanceState.getString(Const.TAG_FLOW);
           ft = getSupportFragmentManager().getFragment(savedInstanceState,"current_fragment");
            mFragmentManager=getSupportFragmentManager();
            mFragmentTransaction = mFragmentManager.beginTransaction();
            bundle= new Bundle();
            bundle.putString(Const.TAG_FLOW, flow);
            bundle.putParcelable(Const.TAG_REQ_BOOKING_DETAILS, bookingDetailsItems);
            ft.setArguments(bundle);
            mFragmentTransaction.replace(R.id.fragment_frame, ft).commit();
        }else{
           // load fragment on first time
        }
    }

So my Question is: Where do I have to save the custom Object(in parent Activity or in fragment) ? When my saved Instance is not null than app crashesh and logs is :

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference

Upvotes: 0

Views: 1813

Answers (3)

Abdul Wasae
Abdul Wasae

Reputation: 3686

You should use ViewModel. ViewModel is specifically made for this purpose.

From the docs:

ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment. It also handles the communication of the Activity / Fragment with the rest of the application (e.g. calling the business logic classes).

Upvotes: 1

Dhruv Tyagi
Dhruv Tyagi

Reputation: 812

use this code in Activity :

    if (findViewById(R.id.fragment_frame) != null) {
        if (savedInstanceState != null) {
fragment =getSupportFragmentManager().getFragment(savedInstanceState,"current_fragment");

        }else{
           // load fragment on first time
        }
    }

and in fragment :

//save custom object

 @Override
    public void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putParcelable("key",customObject);
    }

//now retrieve here

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null)
    customObject= savedInstanceState.getParcelable("key");
}

Upvotes: 0

azizbekian
azizbekian

Reputation: 62209

Take a look at onRetainNonConfigurationInstance() and getLastNonConfigurationInstance()

From docs:

Called by the system, as part of destroying an activity due to a configuration change, when it is known that a new instance will immediately be created for the new configuration. You can return any object you like here, including the activity instance itself, which can later be retrieved by calling getLastNonConfigurationInstance() in the new activity instance.

Upvotes: 0

Related Questions