I have a splash screen which displays for 2-3 sec before it disappears.
I want to add a Fade in Effect when the next activity is loaded. I saw an Example in the Facebook Hacker Example and i am using it.
It uses a finish(); to end that activity to so from the DashboardActivity if some one clicks back it doesnt return back to the SplashAcitivty. But using this doesnt create the Fade in Effect as show in the API demos Examples.
public class SplashActivity extends Activity {
private long splashDelay = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
/*
* this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
* WindowManager.LayoutParams.FLAG_FULLSCREEN);
*/
setContentView(R.layout.activity_splash);
TimerTask task = new TimerTask() {
#Override
public void run() {
finish();
startActivity(new Intent().setClass(SplashActivity.this,
MainActivity.class));
overridePendingTransition(R.anim.fade, R.anim.hold);
}
};
Timer timer = new Timer();
timer.schedule(task, splashDelay);
}
}
Use a handler for this:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this,
MainActivity.class));
overridePendingTransition(R.anim.fade, R.anim.hold);
finish();
}
}, splashDelay);
Related
This question already has answers here:
Avoid splash screen activity when pressing Back button
(6 answers)
Closed 5 years ago.
while pressing back button from home screen then application goes to background and when lifting the app from background, app start from splash screen. but I want to start the app from home screen.
public class SplashActivity extends AppCompatActivity {
private static final long SPLASH_DURATION = 3000L;
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
dismissSplash();
}
};
View rootView = findViewById(android.R.id.content);
rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismissSplash();
}
});
}
#Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(mRunnable, SPLASH_DURATION);
}
#Override
protected void onPause() {
super.onPause();
mHandler.removeCallbacks(mRunnable);
}
private void dismissSplash(){
startActivity(new Intent(this, MainActivity.class));
finish();
}
}
Please Add flag as given below when start activity from splash
Intent intent = new Intent(context, activity);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
Just call finish() method before you call your home activity.
finish();
startActivity(new Intent(getApplicationContext(), yourActivity.class));
if it doesn't work, handle onBackPressed in your home activity.
#Override
public void onBackPressed() {
moveTaskToBack(true); //it goes to background.
}
please use SplashActivity.this instead of this because you are calling dismissSplash from another thread.
private void dismissSplash()
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
SplashActivity.this.finish();
}
i just need to know how is it possible to create a start-up animation in your app. When the app is launched I would like it to go through custom animation and then it reaches the content of the app (main activity) ...
Thanks for all your help
If you really want a "startup" screen, just load up another activity before the MainActivity and display that screen for x amount of time:
public class SplashScreen extends Activity {
// Splash screen timer
private static int TIME_OUT = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, TIME_OUT);
}
}
i have to make an application in which it starts with an animation and if we click the back button then it should return back to application manager.But what i have made in it if u click back button during that animation then it goes to application manager but after a second or two the first page(the one after this animation comes up).
Can anyone help??
This is the animation..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load);
im = (ImageView) findViewById(R.id.load_icon);
rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.load_page);
rotate.setInterpolator(new LinearInterpolator());
im.startAnimation(rotate);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
}
The first page opens because you have added
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
This launches the activity.For knowing if the animation has stopped use AnimationListener. More details here about animation listener
Android, How to set animation listener for view group?
You just added animation to one image view thats all, you do not doing anything with animation. The problem is, you started one thread to start activity P1 after 3 seconds. That thread only starting P1 activity. Try this and try to avoid killProcess(),
public class LauncherActivity extends Activity {
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
XmlParserActivity.class);
startActivity(nextPageIntent);
}
};
mHandler.postDelayed(mRunnable, 3000);
}
/* #Override
public void onBackPressed() {
super.onBackPressed();
mHandler.removeCallbacks(mRunnable);
}*/
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRunnable);
}
}
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This is the answer
I have created the android application with tabhost. In this application, I am having the 4 tabs and each one contains their separate webview. For this application, I want to add SplashScreen for the application before the tab bar's webview is loaded. How can I achieve this?
Create a different activity to show the splash which will be your launcher activity. After launching you can start the tabhost from this activity
Try this
public class SplashScreen extends Activity {
// time for splashscreen
protected int _splashTime = 5000;
private Thread splashTread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final SplashScreen sPlashScreen = this;
// thread for displaying the SplashScreen
splashTread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// wait 5 sec
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
// Go to Main activity
Intent i = new Intent();
i.setClass(sPlashScreen, MainActivity.class);
startActivity(i);
finish();
}
}
};
splashTread.start();
}
}
Hope it helps.
I have created a splash screen for my application. After 5 seconds it starts the next activity using the below code. Now my problem is, if user navigates away from current activity before 5 seconds are over, then as soon as 5 seconds are over the next activity (in my case InfoActivity) comes in front even if I am in another application or anywhere else.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launch);
new Handler().postDelayed(new Runnable() {
public void run() {
final Intent mainIntent = new Intent(LaunchActivity.this, InfoActivity.class);
LaunchActivity.this.startActivity(mainIntent);
LaunchActivity.this.finish();
}
}, 5000);
}
you could use a variable
shouldNavigate=true;
that you unset in the onDestroy() method of your original activity.
onDestroy() {
shouldNavigate=false;
[...]
}
In your postDelayed-run()-method you then check
if(shouldNavigate) {...}
This procedure worked for me.
flag = false;
runnable = new Runnable() {
public void run() {
if(!flag) {
final Intent mainIntent = new Intent(LaunchActivity.this, InfoActivity.class);
LaunchActivity.this.startActivity(mainIntent);
LaunchActivity.this.finish();
}
}
};
handler = new Handler();
handler.postDelayed(runnable, 5000);
onPause() {
super.onPause();
flag = true;
handler.removeCallbacks(runnable);
}
onRestart() {
super.onRestart();
flag = false;
handler.postDelayed(runnable, timeOfPause-timeOfCreate);
}