Move next Activity after animation is finished in Android - android

I am using RotatingTextWrapper in android for text animation. It is working well. But I want to move next Activity after animation is finished automatically by giving time interval. I have used Thread for this purpose and placed animation inside of thread function. Unfortunately Animation is not working but after few second second Activity is opened automatically. Please Help me.
Here is Source Code
public class MainActivity extends AppCompatActivity {
TextView tv;
Timer timer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=findViewById(R.id.txt);
final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Raleway-Light.ttf");
final Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Reckoner_Bold.ttf");
tv.setTypeface(typeface2);
timer=new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
RotatingTextWrapper rotatingTextWrapper = (RotatingTextWrapper) findViewById(R.id.custom_switcher);
rotatingTextWrapper.setSize(30);
rotatingTextWrapper.setTypeface(typeface2);
Rotatable rotatable = new Rotatable(Color.parseColor("#af030f"), 3000, "Xafa", "Hazil", "Uzr","Sizni","Juda");
rotatable.setSize(30);
rotatable.setTypeface(typeface);
rotatable.setInterpolator(new AccelerateInterpolator());
rotatable.setAnimationDuration(600);
Rotatable rotatable2 = new Rotatable(Color.parseColor("#123456"), 3000, "qimoqchimasdim", "Ediku", "So`rayman","Yoqtiraman","ham");
rotatable2.setSize(30);
rotatable2.setTypeface(typeface);
rotatable2.setInterpolator(new DecelerateInterpolator());
rotatable2.setAnimationDuration(600);
rotatingTextWrapper.setContent("? ?", rotatable, rotatable2);
Intent i=new Intent(getApplicationContext(),Main2Activity.class);
startActivity(i);
finish();
}
},4000);
}
}

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//logic
}
}, 4000);
}

Put the animation in the onResume() stage of the activity lifecycle. According to Android documentation for Activity lifecycle onStart() "...makes activity visible to the user..." and onResume() "...app interacts with the user...".
Since you want your animation to be visible to the user, I would place it in onStart() and use the logic of the design to make onResume() handle your animation.
public class MainActivity extends AppCompatActivity{
#OnCreate()
// instantiations class member variables
#OnStart()
// timer code for animation
#OnResume()
Intent i=new Intent(getApplicationContext(),Main2Activity.class);
this.startActivity(i);
}

Related

Android .cancel() doesn't cancel the animation

I'm trying to cancel an animation with a very simply way just to see whether if .cancel() function doing what it is intended to do.
But it seems it just does not work.
Here is the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.my_text_view);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_animation);
textView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animation.cancel();
}
}, 2000);
}
}
The animation is running forever. It just doesn't stop after 2 seconds.
If I call it from the view:
textView.getAnimation().cancel();
It doesn't work either.
What do you think what makes this? I need a proper way to stop an animation.

how to stop open activity while splash screen killed

I have splash screen .
once i open my application the splash screen will appears after completion of splash screen passed intent to HomeActivity.
but when i kill this app while splash screen running after some time HomeScreen will automatically open , but i want to kill the app.
but the HomeScreen should not show when i killed the app .
public class SplashAnimation extends Activity {
ImageView imageViewSplash;
TextView txtAppName;
RelativeLayout relativeLayout;
Thread SplashThread;
MediaPlayer mySong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_view);
mySong=MediaPlayer.create(SplashAnimation.this,R.raw.monn);
mySong.start();
imageViewSplash = (ImageView) findViewById(R.id.imageViewSplash);
txtAppName = (TextView) findViewById(R.id.txtAppName);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
startAnimations();
}
private void startAnimations() {
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.translate);
Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
rotate.reset();
translate.reset();
relativeLayout.clearAnimation();
imageViewSplash.startAnimation(rotate);
txtAppName.startAnimation(translate);
SplashThread = new Thread() {
#Override
public void run() {
super.run();
int waited = 0;
while (waited < 3500) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
waited += 100;
}
SplashAnimation.this.finish();
Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
mySong.stop();
}
};
SplashThread.start();
}
#Override
protected void onStop() {
SplashAnimation.this.finish();
finish();
mySong.stop();
super.onStop();
}
#Override
protected void onDestroy() {
finish();
mySong.stop();
super.onDestroy();
}
}
Once you have called SplashThread.start() it will do its job as long as it can do. I would recommend to use a Handler instead, tho you can remotely cancel the task, the Handler runs:
//init and declare the handler instance
private Handler delayHandler;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (delayHandler == null) {
delayHandler = new Handler();
}
//your code
}
//define the task the handler should do
private void startAnimations() {
//replace the code beginning at 'Thread SplashThread = new Thread()' with the following
delayhandler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
//these flags will prevent to 'redo' the transition by hitting the back button, that also makes calling 'finish()' obsolete
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
//instead of the while loop just execute the runnable after below given amount of milliseconds
}, 3500)
//to remotely cancel the runnable, if the app, respectively the Activity gets killed override 'onDestroy()'
#Override
public void onDestroy() {
super.onDestroy();
mySong.stop();
//calling 'finish()' is obsolete, tho 'finish()' calls 'onDestroy()' itself
//tell the handler to quit its job
delayHandler.removeCallbacksAndMessages(null);
}
Call in onStop() method
SplashThread.interrupt()
You can use Timer instead of instantiating the Thread class.
Refer the code below to start the Activity after 4 seconds. Use this in onCreate() of SplashActivity.
timer = new Timer().schedule(new TimerTask() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
}, 4000);
In your onPause() method use:
timer.cancel()
This will terminate the timer and disregards any currently scheduled tasks.

Android app start up animation

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);
}
}

how to check if an application is killed during an animation in android

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

Android activity instance in memory even after destroying the activity

Eclipse MAT histogram shows SplashActivity instance exists even after launching MemTweaksActivity.Do anybody have any idea about this.
public class SplashActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
new Timer().schedule(new TimerTask() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), MemTweaksActivity.class);
startActivity(i);
finish();
}
},2000);
}
}
But the following code with out any timer successfully removed the SplashActivity instance. MAT shows 0 instance of SplashActivity .
Intent i = new Intent(getApplicationContext(), MemTweaksActivity.class); startActivity(i); finish();
Add logging for the lifecycle calls onPause(), onStop() and onDestroy() in your SplashActivity. You will see that these get called at some point before/after MemTweaksActivity is run. The garbage collector will remove the SpashScreen activity from memory when it feels the need to. You can't force this to happen.
EDIT:
The reason that SplashScreenActivity is still there is because you've not cleaned up the Timer.
Do this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Intent i = new Intent(getApplicationContext(), MemTweaksActivity.class);
startActivity(i);
finish();
// Release timer resources
timer.cancel();
}
},2000);
}

Categories

Resources