Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save interface (Listener) in onSaveInstanceState

SaveInstanceState

For data like Integer, Long, String and else are fine, I just put it in the bundle and get it back once the onCreateView gets called again. But my fragment also has listener like following,

public class SomeFragment extends Fragment {
    public interface SomeListener {
        public void onStartDoingSomething(Object whatItIsDoing, Date when);
        public void onDoneDoingTheThing(Object whatItDid, boolean result);
    }

    private SomeFragmentListener listener;
    private String[] args;

    public static SomeFragment getInstance(SomeListener _listener, String... _args) {
        SomeFragment sf = new SomeFragment();
        sf.listener = _listener
        sf.args = _args

        return sf;
    }

    // rest of the class

    // the example of where do I invoke the listener are
    // - onSetVisibilityHint
    // - When AsyncTask is done
    // - successfully download JSON
    // etc.
} 

How can I have the listener to bundle so that I can get it back later?

like image 316
Tar_Tw45 Avatar asked Jan 10 '14 11:01

Tar_Tw45


People also ask

How do you save onSaveInstanceState?

onSaveInstanceState(): In order to save sates of UI, I override onSaveInstanceState(Bundle savedInstanceState) and save all the data of the UI in savedInstanceState Bundle. This method is called before onStop() in older versions of Android (till android 8.0) and can be called after onStop() for newer versions.

How can you save the state of activity?

When the activity goes into the background, the system calls onSaveInstanceState() . You should save the search query in the onSaveInstanceState() bundle. This small amount of data is easy to save. It's also all the information you need to get the activity back into its current state.

What is the purpose of using onSaveInstanceState () here?

onSaveInstanceState() is called every time the activity is destroyed and recreated, for example during an orientation change or when the device is running on low memory. Use it for saving state variables and taking appropriate action in case activity is killed and recreated.

How can I save my activity data in Android?

To save the data before the activity is destroyed. You can do it in the onSaveInstanceState(Bundle savedInstanceState) method. The data is saved in name value pair format. In this example, the names are my_string, my_boolean, my_double, my_int and my_parcelable.


2 Answers

I recently just found the proper way to do this and I want to share for future reader of this topic.

The proper way to save listener of the fragment is not to save it, but instead, request from activity when fragment got attached to activity.

public class TheFragment extends Fragment {
    private TheFragmentListener listener;

    @Override
    public void onAttach(Context context) {
        if (context instanceof TheFragmentContainer) {
            listener = ((TheFragmentContainer) context).onRequestListener();
        }
    }

    public void theMethod() {
        // do some task
        if (listener != null) {
            listener.onSomethingHappen();
        }
    }

    public interface TheFragmentContainer {
        public TheFragmentListener onRequestListener();
    }

    public interface TheFragmentListener {
        public void onSomethingHappen();
    }
}
  • when the fragment attach to an activity, we check if activity implement TheFragmentContainer or not
  • if the activity does, request listener from activity.
like image 91
Tar_Tw45 Avatar answered Sep 30 '22 00:09

Tar_Tw45


Any Classes without a suitable .put method in Bundle need to implement Serializable (as do any objects used within) or implement Parcelable (the latter is preferred). You can then use the putParcelable or putSerializable methods on Bundle.

like image 35
NigelK Avatar answered Sep 30 '22 02:09

NigelK