1、类的单例化
public class A {
private A() {
}
public static class HOLDER {
public static A instance = new A();
}
public static A getInstance() {
return HOLDER.instance;
}
}
不用再考虑其他什么线程安全是一次同步还是两次同步.
2、点击两次退出应用
public class MainActivity extends Activity {
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toast = Toast.makeText(getApplicationContext(), "确定退出?", 0);
}
public void onBackPressed() {
quitToast();
}
private void quitToast() {
if(null == toast.getView().getParent()){
toast.show();
} else {
System.exit(0);
}
}
}
3、防止Button被频繁点击,同一页面多个Button互斥
public abstract class OnClickEvent implements View.OnClickListener {
public static long lastTime;
public abstract void singleClick(View v);
@Override
public void onClick(View v) {
if (onDoubClick()) {
return;
}
singleClick(v);
}
public boolean onDoubClick() {
boolean flag = false;
long time = System.currentTimeMillis() - lastTime;
if (time < 500) {
flag = true;
}
lastTime = System.currentTimeMillis();
return flag;
}
}
4、android:clipToPadding
android:clipToPadding="false"
android:paddingTop="30dp"
5、如何判断应用被强杀
在Applicatio中定义一个static常量,赋值为-1,在欢迎界面改为0,如果被强杀,application重新初始化,在父类Activity判断该常量的值.
6、应用被强杀如何解决
如果在每一个Activity的onCreate里判断是否被强杀,冗余了,封装到Activity的父类中,如果被强杀,跳转回主界面,如果没有被强杀,执行Activity的初始化操作,给主界面传递intent参数,主界面会调用onNewIntent方法,在onNewIntent跳转到欢迎页面,重新来一遍流程.
未完待续...
网友评论