1

I'm currently parsing a web file then setting 5 textview values upon completion, however, every time the activity is closed and reopened it needs to yet again access the page parse and display the values, how can i prevent it from having to?

It should noted that the data changes quite regularly so i cant simply SAVE the values forever.

public void gatherStockDetails(String symb) {
    AsyncHttpClient client = new AsyncHttpClient();
    client.get("url" + symb + "&f=snpog",
            new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(String response) {
                    Log.e("KFF-a", response);
                    stockinfo = response.split(",");

                    setTextViews(stockinfo[0].replaceAll("[^a-zA-Z0-9.%+ -]+", ""),
                            stockinfo[1].replaceAll("[^a-zA-Z0-9.%+ -]+", ""),
                            stockinfo[2].replaceAll("[^a-zA-Z0-9.%+ -]+", ""),
                            stockinfo[3].replaceAll("[^a-zA-Z0-9.%+ -]+", ""),
                            stockinfo[4].replaceAll("[^a-zA-Z0-9.%+ -]+", ""));
                }

                @Override
                public void onFailure(Throwable arg0, String arg1) {
                    super.onFailure(arg0, arg1);
                }

            });
}

and

public void setTextViews(String Symbol, String Name, String PClose, String Open, String Low) {
        final Animation in = new AlphaAnimation(0.0f, 1.0f);
        in.setDuration(500);

        tvName.setText(Name.replace("Corpora", "Corporation"));
        tvName.startAnimation(in);

        tvSymbol.setText(Symbol);
        tvSymbol.startAnimation(in);

        tvSymbol.setText(PClose);
        tvPClose.startAnimation(in);

        tvLow.setText(Low);
        tvLow.startAnimation(in);

        tvOpen.setText(Open);
        tvOpen.startAnimation(in);
    }

Sorry, i did not mention, this is in a Fragment on an activity, thus there is no onRestoreInstance state.

Broak
  • 4,161
  • 4
  • 31
  • 54
  • See [Transferring Data Using Sync Adapters](http://developer.android.com/training/sync-adapters/index.html) – ramaral Jan 07 '14 at 16:06

3 Answers3

0

Maybe you should put you TextViews in a OnCreate, here is a reference picture:

Process lifespan

This shows where the different modules are loaded, and where a good idea is to put your modules that dont always need reloading.

Picture is loaned from another stackoverflow thread, here: Difference between onCreate()and onStart()

Community
  • 1
  • 1
Paul
  • 478
  • 2
  • 16
0

You can use sharedPreferences. Use a boolean to denote that you don't want page to be parse again and save the textView's text that you got in it. Now everytime the activity opens, check if that boolean is true, if so, just take the values from sharedPreference and assign them to textViews.

If the boolean is false, then parse the page. Whenever you again parse the page, replace old values with new one. This way you don't have to parse the page again and again.

Rohan Kandwal
  • 9,112
  • 8
  • 74
  • 107
0

Maybe you can save some flag in SharedPreferences so you just validate the flag.

public void DataDownloaded()
{
 SharedPreferences sharedPreferences = getSharedPreferences();
 SharedPreferences.Editor editor = sharedPreferences.edit();
 editor.putBoolean("DataDownloaded", true);
 editor.commit();       
}

public Boolean isDataDownloaded() 
{
 SharedPreferences sharedPreferences = getSharedPreferences();
 return sharedPreferences.getBoolean("DataDownloaded", false);
}

and just validate in the onCreate() and the onResume() override methods.

if(!isDataDownloaded)
{
//all your cool stuff
}
Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45
  • Problem is then determining when the preference should deleted in order to get fresh values. – Broak Jan 07 '14 at 15:06
  • If you want some real time connection with the web, you should use a push service, otherwise you could use an aSyncTask that sync the data every amount of time you think is apropiated. – Alejandro Cumpa Jan 07 '14 at 15:12