简体   繁体   中英

use two asynctask and replace the first with the second activity

class activity and splash.class. In the first (which execute the main program), has asynctask (it will be call several time) retrieving data. The second activity is a splash screen which run until the data are downloaded.

public class splash extends Activity {

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

}

private void startHeavyProcessing(){
    new LongOperation().execute("");
}

private class LongOperation extends AsyncTask<String, String, String> {
    Intent i = new Intent(splash.this, MainActivity.class);

    @Override
    protected String doInBackground(String... params) {
        startActivity(i);

        return "";
    }


    protected void onPostExecute(String result) {

    }

    protected void onPreExecute() {

    }

    protected void onProgressUpdate() {}
}
}

I would like to finish spalash activity, when MainActivity finished to retrieve data in its doInBackground. Once done, I would run MainActivity only.

Try this!

private class LongOperation extends AsyncTask<String, String, String> {


@Override
protected String doInBackground(String... params) {

    return "";
}


protected void onPostExecute(String result) {

if(result != null){
}
 Intent i = new Intent(splash.this, MainActivity.class);
 startActivity(i);
}else {
Log.e("DOWNLOAD ERRO");
}

protected void onPreExecute() {

}

protected void onProgressUpdate() {}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM