怎么让程序按照预期的要求去执行呢?
主要是没想到 onResume 这个生命周期方法,废话不多说,直接上代码:
public class SplashActivity extends SupportActivity {
private static boolean isAdClicked = false;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getUrl();
initView();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//延迟三秒再启动 app
start();
}
}, 3000);
}
private void initView() {
mImageView = (ImageView) findViewById(R.id.img_ad);
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击广告图片设置标记并跳转页面
isAdClicked = true;
startActivity(new Intent(SplashActivity.this, AdActivity.class));
}
});
}
private void getUrl() {
//这里是请求广告图片的 Url,并显示
}
private void start() {
//如果点击了广告那么停止启动应用,没点广告的话就按正常情况启动
if (isAdClicked) {
return;
} else if (!TextUtils.isEmpty(SPUtils.getStrSharePre(this, SPKeys.IS_FIRST))) {
startActivity(new Intent(this, MainActivity.class));
} else {
String deviceId = AppUtils.getDeviceId(this);
SPUtils.setSharePre(this, SPKeys.IS_FIRST, deviceId);
startActivity(new Intent(this, GuideActivity.class));
}
finish();
}
@Override
protected void onResume() {
super.onResume();
//从广告页回退时再延迟一点执行启动程序,并改变标记状态
if (isAdClicked) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
start();
}
}, 800);
}
isAdClicked = false;
}
}