David
David

Reputation: 383

Force asynctask to exeute every time app is opened

I need execute the async task every time the app is opened or executed, because I use this Asyntask to fetch some json data from http and in every app execution Must be fresh data.

Any idea how to force this?

Thanks

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

    etResponse = (TextView)findViewById(R.id.etResponse);
    etdia = (TextView)findViewById(R.id.dia);
    etmes = (TextView)findViewById(R.id.mes);

    // check if you are connected or not
    if(isConnected()){

    }

    new HttpAsyncTask().execute("URL TO EXECUTE");
}

Upvotes: 0

Views: 62

Answers (2)

iGio90
iGio90

Reputation: 3301

I saw you solved your issue. Anyway i'll put here the method for exec the asynctask for EVERY activity of the app:

public class myActivity extends Activity {

    private String mLastUrl = "";

    public void execAsync(String url) {
        new HttpAsyncTask().execute(url);
        mLastUrl = url;
    }

    @Override
    protected void onResume() {
        super.onResume();
        execAsync(mLastUrl);
    }

    //your asynctaskcode

On all the other activities you would like to run the asynctask just do this:

public class activityName extends myActivity {

Upvotes: 1

Ogen
Ogen

Reputation: 6709

Place the execute method in your onResume() method.

@Override
    protected void onResume() {
        super.onResume();
        new HttpAsyncTask().execute("URL TO EXECUTE");
    }

Upvotes: 3

Related Questions