0

I am having hard time understanding, how Android app fetches data or refresh data everytime I launch app.

I have data it is update every minute meaning it is changed, but I don't see it update everytime I launch the app, only randomly it updates the data.

I tired, still doesn't work.

OnResume();

Here is my code.

package com.zv.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ZipActivity extends Activity {

    TextView textMsg, textPrompt;
    final String textSource = "https://docs.google.com/spreadsheet/pub?key=0AqSBI1OogE84dDJyN0tyNHJENkNyYUgyczVLX0RMY3c&single=true&gid=0&range=a2%3Aa26&output=txt";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textPrompt = (TextView)findViewById(R.id.textprompt);
        textMsg = (TextView)findViewById(R.id.textmsg);

        textPrompt.setText("Wait...");

        URL CPE;
        try {
            CPE = new URL(textSource);
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader(CPE.openStream()));
            String StringBuffer;
            String stringText = "";
            while ((StringBuffer = bufferReader.readLine()) != null) {
                stringText += StringBuffer + "\n"; ;
            }
            bufferReader.close();
            textMsg.setText(stringText);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            textMsg.setText(e.toString());
        }
        textPrompt.setText("Finished!");        
    }

}
Mowgli
  • 3,422
  • 21
  • 64
  • 88

3 Answers3

1

You can override the Application class in your application. Define this in your manifest file. Every time your app starts -- no matter how it starts, user click, broadcast intent, service -- your Application class will get an on create.

I think you need to understand the life cycle of an application. Applications on android do not die because you close them. If you want it to update every time it is foregrounded you will have to override the onstart() method in your activities.

  • So I can I either write onstart or OnResume() correct? or both ? – Mowgli Dec 06 '12 at 19:33
  • 1
    You can do both. On Resume might be a better option as that would be called after being sent behind another application. I would override the application like i mentioned before and if any of your activities gets a onResume your app can call the common code base in Application. – Delicious Software Dec 06 '12 at 19:37
1

Everytime you start your activity the onCreate method is executed, this means that all the code inside that method will be executed again. If the content that you are fetching is not update is probably because is not updated from the source. But then again the refresh will only happen when you start your activity.

you can check this answer it might help you: https://stackoverflow.com/a/11456974/1552551

Community
  • 1
  • 1
BigBen3216
  • 868
  • 1
  • 8
  • 23
  • Thanks that explain but still need more help, when you say `then again the refresh will only happen when you start your activity` do I need to start activity in code again or physically running app again? can I add OnResume() that will force it to start activity again on resume? such as `protected void onResume() {` ? Thanks – Mowgli Dec 06 '12 at 19:37
  • You can start a thread ,service or asynctask to run every minute and execute the code that updates your views,check my edit.As other users have suggested, you should take a look in the documentation for the lifecycle of an activity . – BigBen3216 Dec 06 '12 at 21:38
1

The Activity is the building block (better, it was, since now the Fragment API represent the base component) of an Android application. It has so-called lifecycle events which you can hook into. You named:

  • onCreate(), called by the framework when the Activity is first created. Possibly, this activity object can stay forever, even when other apps take the foreground and after two weeks the user touch you app's icon in the launcher: it may still be the very same Activity object. In reality, Activites are not so long lived, however you get the idea

  • onResume(), called after onPause(), which in turn is called when the Activity loses the foreground (Note that this has nothing to do with the activity's content visibility). Because onResume() is always called after onPause(), you are guaranteed that it will be called every time the user were doing something else and then switches to your activity, which seems just your use-case.

Note the difference: the user installs your app, touches the icon in the launcher, this fires an intent, the system detects that there's no activity around and creates a new instance. Then calls onResume() and finally takes the activity to the foreground. Next, your receive a SMS notification, you switch to the messanger, then come back to your old activity and onResume() gets called again (but not onCreate).

So if you want to use a lifecycle hook to refresh the Activity's content, onResume() is what you are looking for. However note that once the URL content is read, the content may change and the client won't be notified if the activity still is in the foreground. So you may want to use some sort of server events (push) like C2DM (or websocket, or a plain socket, or whatever you are comfortable with).

Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • Can I make `onResume()` call `onCreate()` will that for my activity to run again which will update the data? – Mowgli Dec 06 '12 at 19:39
  • 1
    No. Only the system calls these methods. Just move the code in `onResume()`, which is called **always** – Raffaele Dec 06 '12 at 19:41
  • I moved my code, it doesn't run the app, it crashes, Don't know what I am doing wrong. – Mowgli Dec 06 '12 at 20:26
  • *crash* means nothing... Provide the stack trace. BTW, for network operations you should use an `AsyncTask` – Raffaele Dec 06 '12 at 23:15