I am developing an Android application where I need to add a splash screen for my application. I usually used only Activityuntil now, but for this project ADT Fragmentalso creates with Activity.
Now I have a confusion when I have to write code timerTaskand Timerto schedule a task to be executed in a onCreatemethod Activityor onCreateViewsomething else?
Currently I have written like this, but I'm not sure if this is right or wrong.
public class SplashActivity extends Activity {
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();
mTimerTask = new TimerTask() {
@Override
public void run() {
}
};
mTimer = new Timer();
mTimer.schedule(mTimerTask, 3000);
}
}
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();
if(mTimer !=null)
mTimer.cancel();
}
}
source
share