내부 저장소의 URL을로드하고 웹에서 해당 콘텐츠를 다운로드하는 시작 화면이 있습니다 (AsynkTask 포함). 다운로드 한 데이터를 ArrayList에 저장하고 주 활동을 호출하고 완료합니다. 주 활동 어댑터는 ArrayList를 관리하고 해당 데이터가 들어있는 ListView를 설정합니다.
메인 액티비티에있는 동안 뒤로 버튼을 누르면 애플리케이션이 종료됩니다 (스플래시 화면 액티비티에 대해서는 android : nohistory = "true"로 설정 함). 그러나 앱으로 돌아 오면 스플래시 화면이 표시됩니다 로드되고 데이터를 다시 다운로드하여 목록보기를 두 배로 만듭니다.
앱으로 돌아올 때 스플래시 화면이로드되지 않도록하려면 어떻게해야합니까?백그라운드 작업이있는 스플래시 화면
초기 화면 번호 :
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 번호 :
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);
}
}
setupStorage() 메소드는 스토리지로부터 파일을로드하고 AsynkTask를 행한다.
아마도 onRestart() 메서드를 재정의하면 해결할 수 있습니까?
또는 AsyncTask가 이미 다운로드 한 데이터를로드하지 못하도록해야합니까?
감사합니다.
비동기 작업 코드를 게시 할 수 있습니다. – Nitish
@Nitish code ed – SirAnderson
Ankit은 데이터를 다운로드하기 전에 데이터가 이미 다운로드되었는지 확인하기 위해 그의 답변에서 아래 제안했습니다. onPostExecute()에 관심이 있었고 finish()를 호출하는 것을 잊었을 수도 있습니다. – Nitish