使用Fragment在Android中启动画面

时间:2014-05-27 09:16:09

标签: android android-fragments fragment

我正在设计一个Android应用程序,我需要添加应用程序的启动画面。通常我到目前为止只使用Activity,但是对于这个项目,ADT也正在使用Fragment创建Activity

现在我有一个困惑,我应该编写timerTaskTimer的代码来安排任务在onCreate ActivityonCreateView中执行方法或别的什么?

目前我写的是这样的,但我不确定这是对还是错。

public class SplashActivity extends Activity {

    // using timer to do operation at certain 3 seconds after.
    private Timer mTimer;
    private TimerTask mTimerTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();

            // execute this after 3 seconds
            mTimerTask = new TimerTask() {

                @Override
                public void run() {
                    // start the activity (Login/Home) depends on the login
                    // status
                }
            };

            mTimer = new Timer();
            mTimer.schedule(mTimerTask, 3000);

        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_splash,
                    container, false);
            return rootView;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // cancel the timer if user has pressed the back button to abort it.
        if(mTimer !=null)
            mTimer.cancel();
    }

}

2 个答案:

答案 0 :(得分:6)

where I should write code of timerTask and Timer to schedule a task to perform either in onCreate of the Activity or onCreateView method or something else ?

创建另一个Activity并编写您的计时器任务代码,然后导航到您的家庭活动。请执行以下操作,

public class MySplash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);   

        new Handler().postDelayed(new Runnable() {
        @Override
        public void run() 
        {
        startActivity(new Intent(MySplash.this,SplashActivity.class));
        finish();
        }
    }, 3000);

        }
    }

然后更改您的主屏幕代码,如下所示,您只需要显示您的片段类。

public class SplashActivity extends Activity {           
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();        
        }
    }

不要忘记在清单文件中添加MySplash并将其设为启动器Activity

注意:根据其他答案,除非需要这么多,否则不建议使用启动画面。

参考,

http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/

答案 1 :(得分:-7)

不要在Android中包含启动画面。这是糟糕的设计。它破坏了用户体验。

用户不喜欢等待。相反,向他们展示您的正常活动,并在ProgressBar或其他内容中添加ActionBar

如果您想要启动屏幕的唯一原因是显示您的徽标和品牌颜色,您应该在ActionBar中执行此操作。根据您的品牌颜色设置ActionBar的样式,并将应用的徽标放在ActionBar的左侧。

http://developer.android.com/design/patterns/help.html#your-app