How to avoid recreating view on onCreate on Android?

前端 未结 11 1758
执笔经年
执笔经年 2021-01-17 22:18

I have a FragmentActivity that shows a contacts list.

Here is my onCreate method:

@Override
protected void onCreate(Bundle          


        
相关标签:
11条回答
  • 2021-01-17 22:57

    The most simple way to achieve this is to do a Nullcheck
    i.e.

    if(yourcomponent==null) {
        initializecomponent();
    }
    


    That may prevent your views from recreating.

    0 讨论(0)
  • 2021-01-17 23:03

    I have faced similar issue once. I am not sure how legitimate this is but the code I have used is below and it fixed a lot:

    private static ViewGroup view1;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            mCurrentActivity = getActivity();
            if (view1 == null) {
                settleFragment(inflater);
            } else {
                if (view1.getParent() != null && view1.getParent() instanceof ViewGroup) {
                    ((ViewGroup) view1.getParent()).removeView(view1);
                }
            }
            return view1;
        }
    

    You can adapte settleFragment(inflater); method with yours. The magic is about static variable named view1. Once the fragment is initialised it is filled up with the data. The other instances of Fragments will simply rip apart view1 from its ex-parent and will paste it on themself.

    It is pretty fast with respect to file storage cache since the object is kept in ram. If OS garbage collects the object, static variable will be null again and while recreating the view no duplicates will occur.

    0 讨论(0)
  • 2021-01-17 23:04

    There's no way you can stop onCreate function from executing. However, you could apply a work-around to avoid duplicate list, you could remove all items from the listview and then let it repopulate the listview.

    0 讨论(0)
  • 2021-01-17 23:06

    I faced same problem while using Fragments.

    We have existing methods as stated above. If you are finding yourself in difficult position for using that then you can try as mentioned below :-

    • Initiate a Boolean value with false.
    • Set that Boolean value to true after you get your data.
    • Now on re-initiate of your OnCreate() check for that Boolean value to be true or false. if false then you need to pull that data again else you need not to get that data. Put condition for same.
    0 讨论(0)
  • 2021-01-17 23:08

    First, the reason your savedInstanceState is null - The system only saves state of activities that are destroyed due to system constraints. If you back out of an Activity it is destroyed for good, and no state will be saved.

    Relevant docs: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

    When your activity is destroyed because the user presses Back or the activity finishes itself, the system's concept of that Activity instance is gone forever because the behavior indicates the activity is no longer needed. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed.

    So, it seems like your particular problem is that while your Activity is gone, your static HumanContact class is still in memory, and your new Activity is loading it with another copy of your contacts.

    There are a couple ways you could solve this. First, you could implement a method on HumanContent to clear out all its items, and call it whenever you launch a new instance of your Activity. This would have the benefit of ensuring your data is up to date, but would mean you have to reload your contacts.

    Secondly, if you wanted to truly avoid reloading the data, I'd recommend creating some sort of cache for the contacts that is independent of the Activity. You should consider your Activity as fairly transient, it can and will be destroyed and recreated frequently, whereas a cache can persist.

    HumanContent seems to be filling this responsibility already, to an extent. You're using it to store your data, and it is persisting beyond the lifecycle of your Activity. You could additionally add a method to it that checks to see if it has contacts loaded, and if not loads them itself. This way it has complete control over loading and caching your data, and Activities can be responsible solely for the display of this data. Be careful with this type of solution that you're not storing too much data in memory, that you're reloading your cache anytime you expect your data to have changed, and be aware that your process might be restarted by the system in some cases, so your cache must be prepared to reload your data in case it is destroyed.

    As for preserving your Views, if the user is backing out of your Activity then the finish() is being called, and your Activity is going to be destroyed. Remember, in this case the system no longer has any concept of the Activity, so there will be no way to preserve these views for reuse.

    0 讨论(0)
提交回复
热议问题