Android 关于finish()、onDestory()、System.exit(0)、onCheckedChanged(RadioGroup arg0, int checkId)总结

一、重写父类finish();函数。

    @Override
    public void finish() {
                //其他结束之前的操作,这里写的手机"back"键都有效

                
                //使用关键字super调用父类的finish()函数
        super.finish();
                //结束动画
        overridePendingTransition(R.anim.push_left_0, R.anim.push_left_1);
    }

这样写的好处如果使用了finish()函数,手机"back"键都有效。如果要保存一些重要的值,或者判断都可以复写这个finish()函数。
finish函数仅仅把当前Activity退出了,但是并没有释放他的资源。安卓系统自己决定何时从内存中释放应用程序。当系统没有可用内存到时候,会按照优先级,释放部分应用。

二、Activity.onDestory()
系统销毁了这个Activity的实例在内存中占据的空间。
在Activity的生命周期中,onDestory()方法是他生命的最后一步,资源空间等就被回收了。当重新进入此Activity的时候,必须重新创建,执行onCreate()方法。

三、System.exit(0)
这个是退出整个应用程序的,是针对整个Application的。将整个程序进程直接关闭退出。

四、onCheckedChanged(RadioGroup arg0, int checkId);

    @Override
    // RadioGroup监听
    public void onCheckedChanged(RadioGroup arg0, int checkId) {
        arg0.check(checkId);
        }

这是一个RadioGroup控件监听实现的接口。但是如果有需要使用这个函数,也可以灵活应用。
1、调用onCheckedChanged(RadioGroup arg0, int checkId);
函数时,需要查找RadioGroup的id,以及查找RadioButton的id。这些值赋值进去。
示例:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
RadioButton radio = (RadioButton) findViewById(R.id.radio);
onCheckedChanged(radioGroup, radio);

2、arg0.check(checkId);

这个作用就是自动帮你选择RadioButton。只要把RadioGroup、RadioButton的id传进去,实现什么都可以了。

 

五、附带常用textview查找id绑定控件并显示text快捷方法

public void setTextViewIdAndShowText(int nRid, String showText){
    try {
         TextView txtTextView = (TextView) findViewById(Rid);
          if (txtTextView != null) txtTextView.setText(txt == null ? "" : txt);
         } catch (ClassCastException fe) {
         }
}

 

posted @ 2015-09-19 09:01  默默笙萧  阅读(1880)  评论(0编辑  收藏  举报