How to avoid recreating view on onCreate on Android?

前端 未结 11 1757
执笔经年
执笔经年 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:43

    It is always adviced to use caching to retain the data . as mentiond by Groucho u can go with storage options Check below link for details.

    http://developer.android.com/guide/topics/data/data-storage.html

    0 讨论(0)
  • 2021-01-17 22:44
    @Override
    public void onPause ()
    {
        super.onPause();
        //...........
    }
    
    @Override
    public void onStop ()
    {
        super.onPause();
        //............
    }
    

    Check if you can use these methods in your fragments ?

    0 讨论(0)
  • 2021-01-17 22:45

    You are going to check controller therefor.

    Instead of that try to check value you stored. First take one variable as Bundle. Save it in onCreate method.

    Bundle bdlSaveInstant;   // Declare at top before onCreate() method.
    /* Initialise in onCreate() method. After 
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_human_list);
    bdlSaveInstant = savedInstanceState;
    */
    
    @override
    onBackPressed()
    {
      bdlSaveInstant.putBoolean("displayed_contacts", true);
    }
    
    @override
    onDestroy()
    {
      bdlSaveInstant.putBoolean("displayed_contacts", true);
    }
    

    Try this will work.

    0 讨论(0)
  • 2021-01-17 22:47

    In your on create method just clear your list.Because every time it will go to on create method and make list clear(which) is your previous data loading.

    OR

    check your list if(list.size==null) then call your API otherwise in else block (list.setAdapter).

    hope it will work.

    0 讨论(0)
  • 2021-01-17 22:51

    when you are done with loading of the data in displayContacts(), you can save a flag(displayed_contacts) in shared preferences. In onCreate() check the preference value, and call your displayContacts() accordingly.

    0 讨论(0)
  • 2021-01-17 22:54

    Its simple. follow these steps in your Activity

    • Declare your listview object and listview_adapter object in side your Activity class.
    • Initialise your listview object and listview_adapter object in OnCreate() method and add the contents in your displayContacts().

    Then definitely you wont get content replication.

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