1

好的,所以我的应用程序上有一个启动画面。我想知道是否有办法让我只在第一次打开应用程序时才出现它(或者如果他们强制关闭它等等),所以如果他们按下后退键,它不会将它们带到初始屏幕。

我可以做到这一点,这样用户就无法通过键入 finish() 来返回启动屏幕,虽然我现在不记得了,但我可以让它只加载一次。一旦我调用了finish(),重新进入应用程序时就会重新出现启动画面。有没有什么办法解决这一问题?

4

2 回答 2

3

Yes, you can do that by calling finish() on the onPause method of your splashActivity.

Or another way to do this is to add android:noHistory="true" on splashActivity of your manifest.xml

于 2013-01-15T01:42:51.383 回答
0

你在SplashScreenActivity浪费时间,因为他们在继续之前会暂停初始化 2-3 秒。

我更喜欢Splash Screen Layout在我的main_activity.xml. 我通过扩展应用程序来检测应用程序的第一次启动。如果是第一次开始,我会在后台构建 UI 时显示我的 Splash-Screen ...(如果 ProgressBar 滞后,请使用后台线程!)

//Extend Application to save the value. You could also use getter/setter for this instead of Shared Preferences...
public class YourApplication extends Application {

    public static final String YOUR_APP_STARTUP = "APP_FIRST_START";

    @Override
    public void onCreate() {
        super.onCreate();

        //set SharedPreference value to true
        SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = mPreferences.edit();
        editor.putBoolean(YOUR_APP_STARTUP, true);
        editor.apply();     
        ...    
     }

检查你的第一次开始MainActivity

public class YourMainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //hide actionbar and other menu which could overlay the splash screen
    getActionBar().hide();

    setContentView(R.layout.activity_main);

    Boolean firstStart = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(TVApplication.YOUR_APP_STARTUP, true);

    if (firstStart) {
        //First app start, show splash screen an hide it after 5000ms
        final RelativeLayout mSplashScreen = (RelativeLayout) findViewById(R.id.splash_screen);
        mSplashScreen.setVisibility(View.VISIBLE);
        mSplashScreen.setAlpha(1.0f);
        final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_container);
        mFrame.setAlpha(0.0f);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
                        R.anim.fade_out_animation);
                fadeOutAnimation.setDuration(500);
                fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        mFrame.setAlpha(1.0f);
                        getActionBar().show();
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mSplashScreen.setVisibility(View.GONE);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                mSplashScreen.startAnimation(fadeOutAnimation);
            }
        }, 5000); //<-- time of Splash Screen shown

    } else {
        ((RelativeLayout) findViewById(R.id.splash_screen)).setVisibility(View.GONE);
        getActionBar().show();
    }

在 main.xml 的顶部插入 SplashScreen。我更喜欢RelativeLayout那个。在示例中,SplashScreen 放置在带有 的布局中Navitgation Drawer,这是我们非常喜欢的,不是吗?

//main_activity.xml
<?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.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- The main content view -->

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- The navigation drawer list -->

        <ListView
            android:id="@+id/slider_list"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_gravity="start"
            android:background="@color/tvtv_background"
            android:choiceMode="singleChoice"
            android:divider="@drawable/nav_bar_divider"
            android:dividerHeight="1dp"
            android:listSelector="@android:color/transparent" />
    </android.support.v4.widget.DrawerLayout>

    <RelativeLayout
        android:id="@+id/splash_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:background="@color/tvtv_white"
        android:visibility="visible" >

        <ImageView
            android:id="@+id/splash_screen_logo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:paddingLeft="50dp"
            android:paddingRight="50dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/splash_screen_text"
            style="@style/TVTextBlueContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_logo"
            android:layout_centerHorizontal="true"
            android:padding="10dp"
            android:text="Awesome splash shiat" />

        <ProgressBar
            android:id="@+id/splash_screen_loader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/splash_screen_text"
            android:layout_centerHorizontal="true"
            android:clickable="false"
            android:indeterminate="true" />
    </RelativeLayout>

</RelativeLayout>   
于 2014-09-02T15:13:41.337 回答