N S*_*rma 5 android fragment android-fragments
我正在设计一个Android应用程序,我需要添加我的应用程序的启动画面.一般来说,我习惯只使用Activity高达到现在,但该项目的ADT是创建Fragment也Activity.
现在我有一个困惑,我应该写的代码timerTask,并Timer安排在执行任何任务onCreate的Activity或onCreateView方法或别的东西吗?
目前我写的是这样但我不确定它是对还是错.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后改变你的主屏幕代码,如下所示,你只需要显示你的片段类.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记MySplash在清单文件中添加它并使其成为启动器Activity.
注意:根据其他答案,除非需要这么多,否则不建议使用启动画面.
参考,
http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/
Mar*_*ema -8
不要在 Android 中包含启动屏幕。这是糟糕的设计。它破坏了用户体验。
用户不喜欢等待。相反,向他们展示您的正常活动并添加一个ProgressBar或ActionBar其他内容。
如果您想要启动屏幕的唯一原因是显示您的徽标和品牌颜色,那么您应该在ActionBar. 根据您的品牌颜色设计您的样式ActionBar,并将您的应用程序的徽标放在ActionBar.
http://developer.android.com/design/patterns/help.html#your-app
| 归档时间: |
|
| 查看次数: |
10785 次 |
| 最近记录: |