Android pre`loading Activity

余生长醉 提交于 2019-12-11 13:45:52

问题


I have a question regarding the loading of the first Activity in my App. Before loading it, app shows this screen: https://www.dropbox.com/s/r33n3u3xfmth345/Screenshot_2013-08-16-12-02-08.png , and what I would like, is to load directly my Activity.

I'll mention that Im using SherlockActivity, and I already tried setting the Theme both in Manifest or programatically in onCreate() of my Activity, with same result (pre-loads with that screen for 2-3 secs, then loads my Activity). Any thoughts ?


回答1:


You have to use the splash screen Activity and after that you have to start your own activity from that Splash screen Activity.

Here is the code for splashActivity.

public class SplashActivity extends Activity {
private int splashTime = 3000;
private Thread thread;
private ProgressBar mSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_splash);
    mSpinner = (ProgressBar) findViewById(R.id.Splash_ProgressBar);
    mSpinner.setIndeterminate(true);
    thread = new Thread(runable);
    thread.start(); 
}
public Runnable runable = new Runnable() {
    public void run() {
        try {
            Thread.sleep(splashTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            startActivity(new Intent(SplashActivity.this,YourActivityName.class));
            finish();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
};
}

Here is code for activity_spalsh.xml file .....

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#48AD83"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="10dp"
    android:gravity="center_horizontal"
    android:text="  your app name "
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#A52A2A" />

<ProgressBar
    android:id="@+id/Splash_ProgressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerInParent="true"
    android:layout_marginTop="5dp" />

</RelativeLayout>


来源:https://stackoverflow.com/questions/18269807/android-preloading-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!