{"id":18763692,"url":"https://github.com/inloop/androidviewmodel","last_synced_at":"2025-04-04T10:08:38.089Z","repository":{"id":21086624,"uuid":"24386514","full_name":"inloop/AndroidViewModel","owner":"inloop","description":"Separating data and state handling from Fragments or Activities without lots of boilerplate-code.","archived":false,"fork":false,"pushed_at":"2019-02-13T17:31:59.000Z","size":984,"stargazers_count":816,"open_issues_count":1,"forks_count":110,"subscribers_count":54,"default_branch":"master","last_synced_at":"2025-03-28T09:08:20.770Z","etag":null,"topics":["android","android-library","architecture","java","mvvm"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inloop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-09-23T19:39:06.000Z","updated_at":"2025-02-17T03:20:30.000Z","dependencies_parsed_at":"2022-09-09T23:23:18.365Z","dependency_job_id":null,"html_url":"https://github.com/inloop/AndroidViewModel","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inloop%2FAndroidViewModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inloop%2FAndroidViewModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inloop%2FAndroidViewModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inloop%2FAndroidViewModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inloop","download_url":"https://codeload.github.com/inloop/AndroidViewModel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247157283,"owners_count":20893220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","architecture","java","mvvm"],"created_at":"2024-11-07T18:27:06.621Z","updated_at":"2025-04-04T10:08:38.066Z","avatar_url":"https://github.com/inloop.png","language":"Java","readme":"AndroidViewModel\n================\n\n\u003cb\u003eImportant notice: Deprecated\u003c/b\u003e\n--------\n\u003cb\u003eThis library served it's purpose for over 3 years. We believe that Google's Android [Architecture Components](https://developer.android.com/topic/libraries/architecture/index.html) are the preferred setup now for new projects. \u003c/b\u003e\n\u003cb\u003e[INLOOPX](http://www.inloopx.com) is dedicated to continue maintaining this library (no deadline on support end). So rest assured that your existing projects don't need be migrated from AndroidViewModel because of this deprecation. We are only stopping new feature development and don't recommend using it for new projects.\u003c/b\u003e\n\n\nSeparating data and state handling from Fragments or Activities without lots of boilerplate-code. Reducing them to simple \u003ci\u003edumb views\u003c/i\u003e.\n\n\u003cb\u003eBasic idea behind this library\u003c/b\u003e.\nAn instance of a ViewModel class is assigned to your Fragment or Activity during the first creation and is kept during it's life cycle, even between display orientation changes. The ViewModel instance is removed after the Fragment or Activity is completely gone (finished, popped from backstack, replaced without keeping it in backstack).\n\nYou can execute asynchronous tasks in this ViewModel instance and this class is not destroyed during orientation change. All data handling and state logic should be placed inside this class. The Fragment or Activity is just a \"dumb\" view.\n\n\nHow to implement\n--------\n\n1. Create an interface for your \u003cb\u003eView\u003c/b\u003e by extending [IView](library/src/main/java/eu/inloop/viewmodel/IView.java). We will call it IUserListView for this example.\n\n   ```java\n    public interface IUserListView extends IView {\n         public void showUsers(List\u003cUser\u003e users);\n    }\n\n   ```\n2. Create your \u003cb\u003eViewModel\u003c/b\u003e class by extending [AbstractViewModel](library/src/main/java/eu/inloop/viewmodel/AbstractViewModel.java). For example: \u003cbr/\u003e\n\n   ```java\n   public class UserListViewModel extends AbstractViewModel\u003cIUserListView\u003e {\n      ....\n   }\n   ```\n3. Each \u003cb\u003eFragment\u003c/b\u003e or \u003cb\u003eActivity\u003c/b\u003e that you would like to associate with a ViewModel will need either to extend [ViewModelActivityBase](library/src/main/java/eu/inloop/viewmodel/base/ViewModelBaseActivity.java)/[ViewModelBaseFragment](library/src/main/java/eu/inloop/viewmodel/base/ViewModelBaseFragment.java) or copy the implementation from these classes to your base activity/fragment class (in case you can't inherit directly). For example: \u003cbr/\u003e\n  \n   ```java\n   public class UserListFragment extends ViewModelBaseFragment\u003cIUserListView, UserListViewModel\u003e \n      implements IUserListView {\n      \n   }\n   ```\n\n4. Also each \u003cb\u003eFragment\u003c/b\u003e or \u003cb\u003eActivity\u003c/b\u003e has to call ```setModelView()``` after the View (Fragment/Activity) was created and initialized. This is usually on the end of onViewCreated (or onCreate in case of an Activity) \u003cbr/\u003e\n  \n   ```java\n   @Override\n   public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {\n        super.onViewCreated(view, savedInstanceState);\n        ButterKnife.inject(this, view);\n        setModelView(this);\n   }\n   ```  \n  \nHow to use\n--------\n\nYou can forward user interaction from the View into the ViewModel simply by calling:\n\n  ```java\n  getViewModel().onDeleteUserClicked(userId);\n  ```\n  \nThe same goes for the opposite direction, when your asynchronous operation in the ViewModel finished and you would like to forward data to the View to show a list for example:\n\n  ```java\n  getViewOptional().showUsers(userList);\n  ```\n\nThe ```getViewOptional()``` method will never return null. It will return a dummy implementation in case the View is null at the moment (e.g. Fragment already destroyed, or between orientation change).\nYou can also check if the View is not null in case you need to:  \n  \n  ```java\n  if (getView() != null) {\n      getView().showUsers(userList);\n  }\n  ```\n\nYour Fragment argument Bundle and Activity intent Bundle is forwarded to the ViewModel's onCreate method, which you can override to read the initial arguments for the ViewModel.\n\n   ```java \n   public void onCreate(Bundle arguments, Bundle savedInstanceState) {\n      long userId = arguments.getInt(\"user_id\", -1);\n   }\n   ``` \n    \n\nData binding support\n--------\nData binding is supported - extend [ViewModelBaseBindingFragment.java](library/src/main/java/eu/inloop/viewmodel/binding/ViewModelBaseBindingFragment.java) instead of ViewModelBaseFragment and implement ```getViewModelBindingConfig()``` in your Fragment.\n\n   ``` java\n   @Override\n   public ViewModelBindingConfig getViewModelBindingConfig() {\n      return new ViewModelBindingConfig(R.layout.fragment_sample_binding, requireActivity());\n   }\n   ```\n\nThat's it. You can then directly use ObservableField in your ViewModels. See [example](sample/src/main/java/eu/inloop/viewmodel/sample/viewmodel/SampleBindingViewModel.java). \n\nSpecial handling for FragmentStatePagerAdapter\n--------\nThe Android implementation of [FragmentStatePagerAdapter](https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html) is removing Fragments and storing their state. This is in contrast with [FragmentPagerAdapter](https://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter.html) where the Fragments are just detached but not removed.\nWe should be also removing ViewModels and storing their state to be consistent with this behaviour.\n\n\u003cb\u003eUse [ViewModelStatePagerAdapter](library/src/main/java/eu/inloop/viewmodel/support/ViewModelStatePagerAdapter.java) instead of the default FragmentStatePagerAdapter\u003c/b\u003e. This class is only overriding the ```destroyItem()``` method and making sure that ViewModel is removed. The state is stored/restored automatically.\nYou can also use the standard FragmentStatePagerAdapter - in that case ViewModels will be kept in memory and removed only when you leave the screen (Activity finished or Fragment removed).\n\nHow does it work?\n--------\n\nA unique global ID is generated for the first time your Fragment or Activity is shown. This ID is passed on during orientation changes. Opening another instance of the same Fragment or Activity will result in a different ID. The ID is unique screen identifier. A ViewModel class is created and bound to this ID. The corresponding ViewModel instance is attached to your Fragment or Activity after an orientation change or if you return to the fragment in the back stack.\nThe ViewModel is discarded once the Fragment/Activity is not reachable anymore (activity is finished or fragment permanently removed).\n\n\nDownload\n--------\n\n```groovy\ncompile 'eu.inloop:androidviewmodel:1.4.0'\n```\n\n## Android Studio Template\nFor faster creating new screens, you can use [Android Studio Template](/template/AVM_Inloop)\n\n![Android Studio Template Window](/template/template-preview.png)\n\n### Install template\n#### Manually:\nCopy the template folder to Android Studio templates folder (`/Applications/Android Studio.app/Contents/plugins/android/lib/templates/others` on Mac)\n#### Automatically:\nRun the following command to download and install the template automatically (Mac only)\n```\ncurl -o androidviewmodel.zip -Lk https://github.com/inloop/AndroidViewModel/archive/master.zip \u0026\u0026 unzip androidviewmodel.zip \u0026\u0026 cp -af AndroidViewModel-master/template/AVM_Inloop/. \"/Applications/Android Studio.app/Contents/plugins/android/lib/templates/other/AVM_Inloop\" \u0026\u0026 rm -r AndroidViewModel-master \u0026\u0026 rm androidviewmodel.zip\n```\n\u003cb\u003eDon't forget to restart the Android Studio.\u003c/b\u003e\n\n### Usage\nIn the Android Studio right click inside the Projet window and select `File \u003e New \u003e AndroidViewModel Inloop \u003e AVM Fragment`\n\n\n![Android Studio New Template](/template/create-new-template-preview.png)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finloop%2Fandroidviewmodel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finloop%2Fandroidviewmodel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finloop%2Fandroidviewmodel/lists"}