Idioms

- Time is precious

Monday, September 12, 2016

Tạo SplashActivity Cho App


Trong quá trình mở app thông thường sẽ tốn một khoảng thời gian ngắn. Tuy ngắn nhưng cũng gây khá khó chịu cho người sử dụng. Do vậy, thay vì ta để người dùng phải chờ trên một màn hình trắng, ta có thể hiển thị một màn hình chờ có thể là logo của app hay vài hình ảnh vui để người dùng cảm thấy thoải mái khi mở app.
Thành quả:



Các bước tiến hành:
- Bước 1: Tạo SplashActivity và đặt theme cho activity này trong manifest như sau:
<activity    android:name=".activity.SplashActivity"    android:theme="@style/splashScreenTheme">    <intent-filter>        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity>

- Bước 2: Tạo splashScreenTheme trong file values/styles.xml
<style name="splashScreenTheme" parent="@style/AppTheme">    <item name="android:windowBackground">@drawable/ic_main</item></style>

- Bước 3: Trong SplashActivity xử lý sự kiện load hoặc gọi activity mới mà mình mong muốn.
public class SplashActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.splashScreenTheme);        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, LoginActivity.class);        startActivity(intent);        finish();    }
}

0 comments:

Post a Comment