#안드로이드에서 로딩화면 구현하기
- 방법1)LoadingActivity → MainActivity
- 방법2)MainActivity → LoadingActivity → MainActivity
방법1) LoadingActivity → MainActivity
1) LoadingActivity 생성
package com.example.n3815.new_app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import com.example.n3815.new_app.assembly.AssemblyListActivity;
/**
* Created by N3815 on 2017-01-31.
*/
public class LoadingActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
startLoading();
}
/**
* 로딩을 위한 메소드 구현 - 2초후에 이동
*/
private void startLoading() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getBaseContext(), AssemblyListActivity.class);
startActivity(intent);
finish();
}
}, 2000);
}
}
run() 부분에 시작할 Activity 설정
또는
/**
* Created by N3815 on 2017-01-31.
*/
public class LoadingActivity extends Activity {
ArrayList<AssemBean> assemblyList = new ArrayList<AssemBean>(); // 전체 국회의원의 정보를 담는 객체
// API 호출을 위한 Client
DefaultRestClient<AssemblyService> restClient;
// 국회의원 REST API URL SERVICE
AssemblyService assemblyService;
boolean isCall = false; // 호출이 되었는지 확인
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading);
try {
if(!isCall) {
// 전체 국회의원 리스트 가져오기
AssemblyList();
}
} catch (IOException e) {
// 다시 요청시도 하기
Toast.makeText(LoadingActivity.this ,"[Loading] Error(10003-요청 오류)",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
/**
* 국회의원 목록 가져오기 I/F
*/
public void AssemblyList() throws IOException {
isCall = true;
restClient = new DefaultRestClient<>();
assemblyService = restClient.getAssemblyClient(AssemblyService.class);
Call<Items> call = assemblyService.getMemberCurrStateList(1, 300);
call.enqueue(new Callback<Items>() {
@Override
public void onResponse(Call<Items> call, Response<Items> response) {
try{
if(response.isSuccessful()){
// 전체 대상자 추가하기
for(AssemBean userInfo : response.body().getItems()){
// 초성정보 객체에 미리 넣어주기
userInfo.setInitialEmpNm(SearchCommon.getInitialSound(userInfo.getEmpNm().charAt(0)));
assemblyList.add(userInfo);
}
Log.v("[AssemblyListActivity]","[API 호출 성공]: "+assemblyList.size());
Log.v("[AssemblyListActivity]","[Adapter에 전달]: 모든 국회의원 정보를 조회 완료하였습니다.");
if(assemblyList.size() == 300){
Intent intent = new Intent(LoadingActivity.this, AssemblyListActivity.class)
intent.putExtra("assem", assemblyList);
startActivity(intent);
}
}
}catch (Exception e){
Toast.makeText(LoadingActivity.this ,"[Loading] Error(10000-파싱 오류)",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(Call<Items> call, Throwable throwable) {
if(throwable instanceof SocketTimeoutException){
// 다시 요청시도 하기
Toast.makeText(LoadingActivity.this ,"서버가 불안정합니다. 다시 시도해주세요.",Toast.LENGTH_LONG).show();
}else{
// 다시 요청시도 하기
Toast.makeText(LoadingActivity.this ,"[Loading] Error(10001-요청 오류)",Toast.LENGTH_LONG).show();
}
throwable.printStackTrace();
}
});
}
주의할 것! finish()가 없을 경우, 뒤로가기 버튼 클릭시 로딩화면으로 다시 되돌아간다.
2) android Manifest.xml 파일에 시작 Activity 수정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.n3815.new_app">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="android.support.multidex.MultiDexApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".LoadingActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name = ".SubActivity"/>
<activity android:name = ".MainActivity"/>
<activity android:name = ".list.ListActivity"/>
<activity android:name = ".customList.CustomListActivity"/>
<activity android:name = ".assembly.AssemblyListActivity"/>
<activity android:name = ".assembly.AssemblyDetailActivity"/>
<activity android:name = ".assembly.AssemblySearchActivity"/>
</application>
</manifest>
시작 Activity를 LoadingActivity로 변경
방법2) MainActivity → LoadingActivity → MainActivity
참고 링크