0

I'm making a to-do list app and after user presses the button I create a new GridLayout(and all the data about time and name of the task inside of it) and add it into my RelativeLayout. How do I save those GridLayouts in UI so after the activity is destroyed and launched again those layouts are there.

After pressing the button I trigger the Create Activity method

public void CreateActivity(String name,int hours, int minutes,int i)
{
 RelativeLayout.LayoutParams relparams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    relparams.addRule(RelativeLayout.BELOW,i);
    relparams.setMargins(0,50,0,100);
   Glayouts.add(new GridLayout(this));
    Glayouts.get(i+1).setBackgroundColor(Color.GRAY);
    Glayouts.get(i+1).setMinimumWidth(relative.getWidth());
    Glayouts.get(i+1).setId(i+1);
    Glayouts.get(i+1).setPadding(10,0,0,0);
    GridLayout.LayoutParams namee = new GridLayout.LayoutParams();
    namee.columnSpec = GridLayout.spec(0);
    namee.rowSpec = GridLayout.spec(0);
    namee.setGravity(Gravity.LEFT);
    final TextView Actname = new TextView(this);
    Actname.setText(name);

    GridLayout.LayoutParams checkbox = new GridLayout.LayoutParams();
    checkbox.columnSpec = GridLayout.spec(1);
    checkbox.rowSpec = GridLayout.spec(0);
    checkbox.setGravity(Gravity.RIGHT);
    CheckBox check = new CheckBox(this);
    // ADDING TO LAYOUT
    Glayouts.get(i+1).addView(Actname,namee);
    Glayouts.get(i+1).addView(check,checkbox);

    relative.addView(Glayouts.get(i+1),relparams);
Miltent
  • 185
  • 1
  • 9
  • Can you explain it more clearly, or post any UI. What exactly you want to achieve? – Sandeep Insan Nov 17 '18 at 15:06
  • Thanks for responding, I added my method that adds the gridlayout. Basically it creates a gridlayout inside a relative one, but when I shut down my app and launch it again my relative layout is empty again. How do I save that GridLayout so its gonna be there after launching the app again – Miltent Nov 17 '18 at 15:19

2 Answers2

1

Theoretically when you extends View, then you can also override onSaveInstanceState and onRestoreInstanceState methods, where you must provide your own SavedState class that typically extends BaseSavedState. You can find info on that here

In your case, your layout is dynamic, therefore this doesn't really work. To tell you the truth, your layout probably shouldn't be constructed this way, you should be rendering the grid using a RecyclerView based on a "model" that describes this layout, render the items of the grid via the RecyclerView.Adapter, and you should persist either the "model", or the data you use to construct this model along with the user-inputted state so that you can re-construct the model that will be rendered via your RecyclerView.

You can read more about RecyclerView here.

You can read more about data persistence here.

You can read about using onSaveInstanceState to save data in Activities/Fragments across config change and process death (but not finishing then restarting the app) here.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

You can’t. The best way to save state is to use some persistence mechanism, for example database (I’d recommend Room as it is officially supported by Google).

After clicking a button, you should put all the needed information (name, hours, minutes) in the database and when Activity is created, you can read all persisted data and - basing on it - create all needed layouts again.

Another option is storing data in SharedPreferences - it is much easier to setup, so you can also start with this solution. Please note, I'm suggesting it as a first step in the world of persistency in Android, not as a preferred solution for storing data.

  • Can you explain downvoting? – Piotr Aleksander Chmielowski Nov 17 '18 at 15:46
  • Views do have a built-in state persistence mechanism, so "you can't" is a stretch, even if it is not applicable here (because of programatically constructing the layout). Also because you said "store data in SharedPreferences" which is honestly just a "no-no" in general. It might be a key-value store, but in reality it *is* just a big XML file. – EpicPandaForce Nov 17 '18 at 18:47
  • Thanks for explanation, let me refer to it: 1. OP asked for a mechanism for keeping states, even after app is launched again (has explained it in comment). Build-in view state persistence state does not provide such functionality. 2. I'm also sure that Room (or Realm or other solutions) is much better than `SharedPreferences` for real apps (I've even recommended it in my answer), hovewer IMO, for beginner it's a quite good solution, as is the easiest one to start with. Your opinion can differ, however I don't see a reason to downvote. – Piotr Aleksander Chmielowski Nov 17 '18 at 19:07
  • @EpicPandaForce based on your comment, I've edited my answer to clarify that `SharedPreferences` is not a target solution. – Piotr Aleksander Chmielowski Nov 17 '18 at 19:10
  • 1
    Hmm I initially thought he meant when you put app to background (with the process getting terminated) then come back. Which Views *do* handle. But I guess that was a stretch :D your revised answer is better so I revoked it – EpicPandaForce Nov 17 '18 at 19:16