-3

I want to know how can I save all changes in an activity and restore them?

for example:
I have a list view in my activity which have elements like String and image. and I can delete items of this list view. Now I want if I deleted or created new list view items in my activity and closed app, in next time that I opened app all changes be loaded from storage of device.

is there any way to do this?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_layout_dialogs);
    dialogsList = (DialogsList) findViewById(R.id.dialogsList);
    Button adddialog = (Button)findViewById(R.id.btn_dialog);

    adddialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            initAdapter();
        }
    });
    savedInstanceState.clone();
}
  • 2
    Why the SHOUTING in the title? – EJoshuaS - Stand with Ukraine Apr 05 '18 at 21:16
  • Please see: [How do I avoid misusing tags?](https://meta.stackoverflow.com/questions/354427/how-do-i-avoid-misusing-tags) as well as [ask]. – EJoshuaS - Stand with Ukraine Apr 05 '18 at 21:16
  • You guys need to be more welcoming when new people join the community instead of rushing to down vote. – MoGa Apr 05 '18 at 21:19
  • 3
    @MoGa: yes we should be [nice to new programmers and new members to this site](https://meta.stackoverflow.com/questions/328269/should-i-be-nice-to-new-users), but yes, we should also down-vote low-quality questions if we find them, whether new or not. Remember that on this site, the only thing that matters in voting is the quality of the question and the answer, period. It does not matter if the poster is new or has been here for years, the quality of the question/answer is the coin of the realm. Hopefully down-voted questions will be improved and then up-voted. – Hovercraft Full Of Eels Apr 05 '18 at 21:23
  • 3
    We moderate content, not users. – EJoshuaS - Stand with Ukraine Apr 05 '18 at 21:26
  • any way if i did some thing wrong i apology for that – script_kiddo Apr 05 '18 at 21:31
  • 2
    Possible duplicate of [Saving Android Activity state using Save Instance State](https://stackoverflow.com/questions/151777/saving-android-activity-state-using-save-instance-state) – fastr.de Apr 05 '18 at 21:41
  • @script_kiddo: yes, you've asked a poorly researched question, or if you have done full research, you've not shown any of it in the question itself. Please go through the [help] and the [ask] to see how to better use this site. – Hovercraft Full Of Eels Apr 05 '18 at 21:48
  • Just to point out, while i answered you, i completely agree with the above. Try improving your writing, remove caps, search a bit more before asking and things will go pretty fine. There *are* several similar questions in SO already. Also, you seem to struggle with language. Some languages are supported in specific stack overflow localizations. There is a portuguese version, for exemple. Check out if you can find one of your native language, if it helps you lose this barrier. – Elindor Apr 06 '18 at 13:35

1 Answers1

0

Whoa whoa, careful with the store all changes in activity idea.

The title alone sounds bad enough so that people will roll their eyes. What you actually want is called Data Persistance, and yes it is very much possible.

Not for saving a whole activity because that is simply misusage of code, but you can store small chunks of data. To your luck, you want exactly that.

There are several ways to do it. Local database is one, but somewhat complex. You seem to be doing it for learning from the very beggining, so i recommend Shared Preferences.

Basically, you will need to get all data from the list view into an array, and store this array with shared preferences for a chosen key (Any name you give to the key is valid). Whenever you open the list view again, read with shared preferences for the key of same name, and you will get that array. Then you fill the list view with the recently obtained array.

Elindor
  • 175
  • 2
  • 8