mercredi 6 mai 2015

How can I keep an Android activity from reverting back to how it was initially created?

I'm making an app that stores a list of things. The first activity starts with a TextView with "Empty", and a + button in the action bar. There is a ListView set to GONE. I want it so that when I create a new entry, the empty TextView will disappear, and the ListView will appear, with the new entry.

I tried this three ways:

  1. Two different layouts. First one with the TextView that was set in the onCreate function. The + button opens a dialog, and when the user clicks "Create" (positive button), it checks if the list is empty. If so, it uses setContentView(id) to load the new layout with the ListView.

  2. One layout, with the "Empty" TextView with visibility set to visible, and a ListView set to gone. When the "Create" button of the dialog is clicked, it checks if the list is empty. If so, it sets the ListView to Visible, and the TextView to Gone.

  3. Similar to the second method, only I have the ListView set to Visible and the TextView set to gone. In the onCreate method, I set the TextView to Visible, and ListView to Gone. I was thinking if it was going by the default layout values, this would work.

In all 3 of these methods, the first activity reverts back to normal. The problem is after the User creates a new item, the activity opens a new activity with the item information. When I back out of the second activity, the first activity is back to normal with a visible Empty TextView, and a hidden ListView.

It seems when I go back from the first activity, the onCreate method gets run again. Is there some way to keep the activity in the background or is there something else I'm missing?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire