Reputation: 2401
In my main splash activity, I have a handler that calls runnable which delays the next activity from starting. I have removed my static references, but I know that my handler is causing a memory leak.
public class MainSplashActivity extends AppCompatActivity {
private int SPLASH_TIME_OUT = 5000;
private TextView heyDj;
private ImageView spinninRecords;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heyDj = (TextView) findViewById(R.id.hey_dj_intro);
spinninRecords = (ImageView) findViewById(R.id.image_album_splash);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein);
heyDj.startAnimation(myFadeInAnimation);
spinninRecords.startAnimation(myFadeInAnimation);
Animation mySpinAnimation = AnimationUtils.loadAnimation(this, R.anim.spin_logo);
spinninRecords.startAnimation(mySpinAnimation);
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
Intent homeIntent = new Intent(getApplicationContext(), LandingActivity.class);
startActivity(homeIntent);
finish();
}
}, SPLASH_TIME_OUT);
}
@Override
protected void onDestroy()
{
super.onDestroy();
}
}
What is the proper way of creating a handler with a runnable, so it doesn't leak?
Upvotes: 2
Views: 198
Reputation: 1007584
All things being equal, if you have an option of not using a Handler
, do not use a Handler
.
In this case, since postDelayed()
is available on View
, you can switch to that and avoid any Handler
-related shenanigans.
Upvotes: 1