문제

I have a splash screen that loads URLs from the Internal Storage and downloads their content from the Web (with an AsynkTask). It puts the downloaded data into an ArrayList, calls the main Activity and finishes. The main activity adapter manages the ArrayList and sets a ListView containing its data.
While I'm in the main Activity, if I press the back button the application exits (I set the android:nohistory="true" for the splash screen activity), but when I return to the app, the splash screen gets loaded and downloads the data again, "doubling" the list view.
How can I prevent the splash screen to be loaded when I return to the app?

Splash screen code:

Context mContext;
ProgressBar progress = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.activity_launcher);

    progress = (ProgressBar)findViewById(R.id.progress);
    progress.setIndeterminate(true);


    if(canWriteOnExternalStorage()) {

        try {
            setupStorage();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else 
    //dialog appears


}

AsynkTask code:

private class LoadGames extends
AsyncTask<String, Integer, Boolean> {

    private ProgressDialog mProgressDialog = null;
    private String remoteUrl = null;

    @Override
    protected void onCancelled() {
        Log.e(com.example.ludos2_0.MainActivity.TAG,
                "AsyncTask->LoadGames: onCancelled !");
        super.onCancelled();
    }

    @Override
    protected void onPreExecute() {
        Log.d(com.example.ludos2_0.MainActivity.TAG,
                "AsyncTask->LoadGames: onPreExecute !");

    }

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

        if (params.length == 0)
            return false;
        else
            for (int k = 0; k < (params.length)/2; ++k)
            {
                this.remoteUrl = params[k*2];

                Log.d(com.example.ludos2_0.MainActivity.TAG,
                        "AsyncTask->LoadGames: doInBackground ! ("
                                + this.remoteUrl + ")");

                // HTTP Request to retrieve the videogames list in JSON format
                try {

                    // Creates the remote request
                    Log.d(com.example.ludos2_0.MainActivity.TAG,
                            this.remoteUrl);
                    RESTRequest request = new RESTRequest(this.remoteUrl);
                    request.isMethodGET(true);

                    // Executes the request and print the received response
                    String response = RESTRequestExecutor.execute(request);

                    // Custom/Manual parsing using GSON
                    JsonParser parser = new JsonParser();

                    if (response != null && response.length() > 0) {
                        Log.d(com.example.ludos2_0.MainActivity.TAG, "Response: "
                                + response);
                        JsonObject jsonObject = (JsonObject) parser.parse(response);

                        JsonObject itemObj = jsonObject.getAsJsonObject("results");

                        String id = null;
                        String title = null;
                        String thumbnail = null;
                        String description = null;
                        String image = null;
                        String platform = null;

                            id = itemObj.get("id").getAsString();
                            title = itemObj.get("name").getAsString();

                            if (!(itemObj.get("image").isJsonNull()))
                            {
                                thumbnail = ((JsonObject)itemObj.get("image")).get("tiny_url").getAsString();
                                image = ((JsonObject)itemObj.get("image")).get("small_url").getAsString();
                            }
                            else 
                            {
                                thumbnail = "http://www.persicetometeo.com/images/not_available.jpg";
                                image = "http://www.persicetometeo.com/images/not_available.jpg";
                            }
                            description = itemObj.get("deck").getAsString();        

                            platform = params[k*2 + 1];

                            Log.d(com.example.ludos2_0.MainActivity.TAG,
                                    title);

                            ListsManager.getInstance().addVideogame(new Videogame(id, title, thumbnail, image, description, platform));



                    } else {
                        Log.d(com.example.ludos2_0.MainActivity.TAG,
                                "Error getting response ...");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(com.example.ludos2_0.MainActivity.TAG,
                            "Exception: " + e.getLocalizedMessage());
                }

            }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        Log.d(com.example.ludos2_0.MainActivity.TAG,
                "AsyncTask->LoadGames: onPostExecute !");

        progress.setVisibility(View.GONE);

        if (result == false) {
            Log.e(com.example.ludos2_0.MainActivity.TAG,
                    "AsyncTask->LoadGames: Error Downloading Data !");
        } else {
            Log.d(com.example.ludos2_0.MainActivity.TAG,
                    "AsyncTask->LoadGames: Data Correctly Downloaded !");
            Intent intent = new Intent(mContext, MainActivity.class);
            startActivity(intent);
            finish();
        }

        super.onPostExecute(result);
    }
}

The setupStorage() method loads the file from the Storage and executes the AsynkTask.
Maybe could the overriding of the onRestart() method be a solution?
Or should I prevent the AsyncTask from loading the data already downloaded?
Thanks!

도움이 되었습니까?

해결책

It would be better to prevent AsynkTask to download it again. Or better to clear your listview data. Means if use ArrayList with your List adapter then just clear it before storing putting new data.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top