0

I have a list of items that are created from the results of website parsing using Jsoup, a task started when an image button is pressed. The results appear fine but when I press the back button on the phone and then go back into the activity the results disappear and the screen is blank. This means the user will have to press the button again.

So how do I go about preserving the list of items only once the image button has been pressed?

Thanks

Edit

I have looked at the Data Storage page on the Android site and have done the following:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView( R.layout.new_sightings );

    // Store data once created
    SharedPreferences settings = getSharedPreferences( PREFS_NAME,0 );

    reload = (Button)findViewById( R.id.reloadTen );
    reload.setOnClickListener( this );

    list = getListView();
    list.setOnItemClickListener( new OnItemClickListener() 
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id ) 
        {
            // Start an activity based on what list view item is pressed
            Intent intent = new Intent( newSightings.this, newCompass.class );

            // Pass the data we retrieved to the next activity
            intent.putExtra( "info",data[position]  );
            startActivity( intent );
        }

    });
    list.setTextFilterEnabled( true );
    adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
    setListAdapter( adapter );
}

@Override
protected void onStop() 
{
    super.onStop();

    SharedPreferences settings = getSharedPreferences( PREFS_NAME,0 );
    SharedPreferences.Editor editor = settings.edit();

    for( int i = 0; i < data.length; i++ )
    {
        editor.putString( "sighting"+i,data[i].toString() );
    }
    editor.commit();
}

I know I haven't done much in the onCreate method as Im not really sure how to call the data back properly. There is a lot of data and the values are quite different for each key. Btw should i add to the shared preferences during the AsyncTask that actually gets the data? I'm a little confused on this one...

Katana24
  • 8,706
  • 19
  • 76
  • 118
  • Is that all one Activity? Or does Activity A hold a button, and Activity B show the list of items? – pouzzler Apr 01 '12 at 21:18
  • Yeah it's all one activity. When the image button is pressed it gets the data and places it on the screen. – Katana24 Apr 01 '12 at 21:24

3 Answers3

1

Well when you use back, the activity is gone. Therefore you need to hard-save the data. this could help.

Then, in onCreate() check if you have data saved, and if so, load it.

Best regards.

pouzzler
  • 1,800
  • 2
  • 20
  • 32
1

You have two options to save such information:

  1. Right after the parsing is done. You may use SharedPreferences to save the data as soon as it's shown to the user.
  2. You may intercept back key press, see Intercept back button from soft keyboard

I'd choose #1, because #2 is much more hassle. Consult Activity Lifecycle documentation to understand when to restore your saved information.

Community
  • 1
  • 1
devmiles.com
  • 9,895
  • 5
  • 31
  • 47
  • Yeah I looked at the back key and it seems to be the most convoluted method. I successfully stored the user location data based on when the current activity is obscured as outlined in the doc link pouzzler directed me to. When I place the list view in there I'll post what I did. Thanks again :D – Katana24 Apr 02 '12 at 18:01
1

Before using shared preference, Can you tell How much you want to save it. If you want to save few data then gohead and save it but if you have a huge amount of data then dont prefer go with shared preference. Store the data in database when ever you retrive the data from the server on button click and retrive it when ever required.

On trival to your problem you are "listitems" the place where you are saving your data, So just make it static and try must also solve your problem.

Both the above must work for your scenario.

Ishu
  • 5,357
  • 4
  • 16
  • 17