Simple Save State Instance.
Add maven jitpack.io and dependencies in build.gradle (Project) :
// build.gradle projectallprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// build.gradle app/moduledependencies {
...
implementation 'com.github.gzeinnumer:KeepStateUI:version'//required for example
implementation 'com.github.gzeinnumer:RecyclerViewAdapterBuilder:2.0.1'
}- Activity StateUI
- Fragment StateUI
- Recyclerview StateUI
- Image StateUI
- Variable With StateUI
- Clear Value StateUI onStop
- Clear All StateUI
Save UI State like SavedInstanceState with Bundle. but with this library, you can keep data/value in View as long as you want,
or you can Clear Cache from your app in Application Settings. State will keep save even when you kill your app process.
publicclassMainActivityextendsAppCompatActivity {
privateStateUIstateUI;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
...
stateUI = StateUIBuilder.Build(MainActivity.class, getApplicationContext());
}
}Use function addView(KEY, VALUE) on onPause() add value that you want to keep and use saveState() to submit your value. When onPause() called StateUI will keep your value.
publicclassMainActivityextendsAppCompatActivity {
privateStateUIstateUI;
...
@OverrideprotectedvoidonPause() {
super.onPause();
stateUI.addView("binding.edUsername", binding.edUsername.getText().toString());
stateUI.addView("binding.edPass", binding.edPass.getText().toString());
stateUI.saveState();
}
}Use function onResume() to get value that you have been save before. Check value in StateUI is exists, if exists than get value with it's own KEY with getValue(KEY) and set value to your view again.
publicclassMainActivityextendsAppCompatActivity {
privateStateUIstateUI;
...
@OverrideprotectedvoidonResume() {
super.onResume();
if (stateUI.getState()) {
StringuserName = stateUI.getValue("binding.edUsername");
binding.edUsername.setText(userName);
Stringpass = stateUI.getValue("binding.edPass");
binding.edPass.setText(pass);
}
}
}Clear your state when you no need it anymore with.
//login and clear last value on form by onClickbinding.btnLogin.setOnClickListener(v -> {
stateUI.clearState();
startActivity(newIntent(getApplicationContext(), MenuActivity.class));
});Here is Full Code MainActivity.java & activity_main.xml
Preview:
![]() | ![]() | ![]() | ![]() |
|---|---|---|---|
(Before) Data lost in onBackPressed() | (Before) Data lost in onDestroy() | (After) Data keep in onBackPressed() | (After) Data keep in onDestroy() |
Seems like Activity StateUI. but you need to use onViewCreated and Overridepublic onPauseonResume() and public onResume().
publicclassHomeFragmentextendsFragment {
privateStateUIstateUI;
@OverridepublicvoidonViewCreated(@NonNullViewview, @NullableBundlesavedInstanceState) {
...
stateUI = StateUIBuilder.Build(HomeFragment.class, requireContext());
}
@OverridepublicvoidonPause() {
super.onPause();
stateUI.addView("binding.edUsername", binding.edUsername.getText().toString());
stateUI.addView("binding.edPass", binding.edPass.getText().toString());
stateUI.saveState();
}
@OverridepublicvoidonResume() {
super.onResume();
if (stateUI.getState()) {
StringuserName = stateUI.getValue("binding.edUsername");
binding.edUsername.setText(userName);
Stringpass = stateUI.getValue("binding.edPass");
binding.edPass.setText(pass);
}
}
}Here is Full Code HomeFragment.java & fragment_home.xml
Preview:
![]() | ![]() | ![]() | ![]() |
|---|---|---|---|
(Before) Data lost in onBackPressed() | (Before) Data lost in onDestroy() | (After) Data keep in onBackPressed() | (After) Data keep in onDestroy() |
You can save List to StateUI. Use function addViewList(KEY, ListStateReceiver<MyModel>) to set value and getValueList(KEY, ListStateCallBack<MyModel>) to get value.
publicclassRecyclerViewActivityextendsAppCompatActivity {
privateList<MyModel> list = newArrayList<>();
privateStateUIstateUI;
...
@OverrideprotectedvoidonPause() {
super.onPause();
stateUI.addViewList("binding.rv", newListStateReceiver<MyModel>() {
@OverridepublicList<MyModel> listReceived() {
returnlist;
}
});
stateUI.saveState();
}
@OverrideprotectedvoidonResume() {
super.onResume();
if (stateUI.getState()) {
stateUI.getValueList("binding.rv", newListStateCallBack<MyModel>() {
@OverridepublicTypesetListModel() {
// need converter for Gson, put your ModelPojo on TypeTokenreturnnewTypeToken<List<MyModel>>(){}.getType();
}
@OverridepublicvoidlistCallBack(List<MyModel> listFromState) {
list = newArrayList<>(listFromState);
initAdapter();
}
});
}
}
}Here is Full Code RecyclerViewActivity.java & MyModel.java & activity_recycler_view.xml
Preview:
![]() | ![]() | ![]() | ![]() |
|---|---|---|---|
(Before) Data lost in onBackPressed() | (Before) Data lost in onDestroy() | (After) Data keep in onBackPressed() | (After) Data keep in onDestroy() |
You can save Bitmap from your ImageView. Use function addViewBitmap() to set value and getValueBitmap() to get value.
publicclassImageActivityextendsAppCompatActivity {
privateStateUIstateUI;
...
privatevoidloadImageGlide() {
StringimgUrl = "https://avatars3.githubusercontent.com/u/45892408?s=460&u=94158c6479290600dcc39bc0a52c74e4971320fc&v=4";
Glide.with(this).load(imgUrl).error(R.mipmap.ic_launcher).listener(newRequestListener<Drawable>() {
@OverridepublicbooleanonLoadFailed(@NullableGlideExceptione, Objectmodel, Target<Drawable> target, booleanisFirstResource) {
returnfalse;
}
@OverridepublicbooleanonResourceReady(Drawableresource, Objectmodel, Target<Drawable> target, DataSourcedataSource, booleanisFirstResource) {
try {
stateUI.addViewBitmap("binding.img", (BitmapDrawable) resource);
} catch (Exceptione) {
e.printStackTrace();
}
returnfalse;
}
}).into(binding.img);
}
privatevoidloadImagePicasso() {
StringimgUrl = "https://avatars3.githubusercontent.com/u/45892408?s=460&u=94158c6479290600dcc39bc0a52c74e4971320fc&v=4";
Picasso.get().load(imgUrl).into(binding.img, newCallback() {
@OverridepublicvoidonSuccess() {
try {
stateUI.addViewBitmap("binding.img", (BitmapDrawable) binding.img.getDrawable());
} catch (Exceptione) {
e.printStackTrace();
}
}
@OverridepublicvoidonError(Exceptione) {
}
});
}
@OverrideprotectedvoidonPause() {
super.onPause();
stateUI.saveState();
}
@OverrideprotectedvoidonResume() {
super.onResume();
if (stateUI.getState()) {
Bitmapbitmap = stateUI.getValueBitmap("binding.img");
if (bitmap != null) {
binding.img.setImageBitmap(bitmap);
}
}
}
...
}You can save Path from your ImageView. Use function addViewPath() to set value and getValuePath() to get value.
Note : This method Required Permission Storage to read file
publicclassImageActivityextendsAppCompatActivity {
privateStateUIstateUI;
...
@OverrideprotectedvoidonActivityResult(intrequestCode, intresultCode, @NullableIntentdata) {
...
//take image with camera or explisit intentStringpath = "/storage/emulated/0/ExternalFolder/Foto/JPEG_FILE_NAME.jpg";
stateUI.addViewPath("binding.img", path);
...
}
@OverrideprotectedvoidonPause() {
super.onPause();
stateUI.saveState();
}
@OverrideprotectedvoidonResume() {
super.onResume();
if (stateUI.getState()) {
Stringpath = stateUI.getValuePath("binding.img");
if (path != null){
Glide.with(this).load(newFile(path)).error(R.mipmap.ic_launcher).into(binding.img);
}
}
}
...
}Here is Full Code ImageActivity.java & activity_image.xml
Preview:
![]() | ![]() | ![]() | ![]() |
|---|---|---|---|
(Before) Data lost in onBackPressed() | (Before) Data lost in onDestroy() | (After) Data keep in onBackPressed() | (After) Data keep in onDestroy() |
Variable
//onPause()Stringdata = "sentThisDataToState";
stateUI.addView("data", data);
//onResume()Stringdata = stateUI.getValue("data");ViewModel
//onPause()stateUI.addView("vm.data.getValue()", vm.data.getValue());
//onResume()Stringdata = stateUI.getValue("vm.data.getValue()");
vm.setData(data);READ CAREFULLY!!! IF YOU USE THIS STEP. APP WILL KILL STATE-UI IN YOUR CURRENT ACTIVITY WHEN YOU CLOSE(NOT MINIMIZE) YOUR APP, ONLY ON CURRENT ACTIVITY
You can clear StateUI if you WONT keep it when app killed, put stateUI.clearState() on function onStop().
//ignore this if you want to keep value even when your app Killed@OverrideprotectedvoidonStop() {
super.onStop();
stateUI.clearState();
}Call this function every where, this function will Clear All Of Your StateUI that you have been save before, in every Activity/Fragment
stateUI.destroyStateUI();- 1.0.0
- First Release
- 1.0.1
- Clear All StateUI
- 1.0.2
- Image
- 2.0.0
- Support SDK 12
You can sent your constibution to branchopen-pull.
Copyright 2021 M. Fadli Zein
















