Activity

作者: 九品小芝麻 | 来源:发表于2016-05-05 11:53 被阅读0次

Activity

生命周期

(启动)onCreate    (不可见)  ->  onStart  (可见)  ->    onResume    (进入前台)
(销毁)onDestroy   <-  (停止,onRestart) onStop   <-  (进入后台)onPause

Activity切换

A.startActivity(B);
A.onPause()
B.onCreate()
B.onStart()
B.onResume()
A.onSaveInstanceState()
A.onStop()

home和back

点击home键当前activity会被暂停,进入后台。
back键系统会销毁当前activity。

Activity配置

在启动Activity,AMS会检查待启动Activity是否在manifest.xml中配置过,如果没有进行配置,则跑出异常:ActivityNotFoundException

    <activity
            android:name=".ui.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ui.SubActivity"
            android:screenOrientation="portrait" />

Activity启动

显示

Intent intent = new Intent(Context, Class);
startActivity(intnet);

隐示

数据传递

        Intent intent = new Intent(this,SecondActivity.class);
        //1. 直接设置,内部也是使用Bundle
        intent.putExtra("key", "value");

        //2. 使用bundle
        Bundle bundle = new Bundle();
        bundle.putString("key", "value");
        intent.putExtra("bundle",bundle);
        startActivity(intent);

Intent的源码

public class Intent implements Parcelable, Cloneable {
private Bundle mExtras;
public Intent putExtra(String name, String value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putString(name, value);
        return this;
    }
}

Bundle

public final class Bundle extends BaseBundle implements Cloneable, Parcelable {
    public void putString(@Nullable String key, @Nullable String value) {
        unparcel();
        mMap.put(key, value);
    }
}

public class BaseBundle {
    
    ArrayMap<String, Object> mMap = null;

    BaseBundle(@Nullable ClassLoader loader, int capacity) {
        mMap = capacity > 0 ?
                new ArrayMap<String, Object>(capacity) : new ArrayMap<String, Object>();
        mClassLoader = loader == null ? getClass().getClassLoader() : loader;
    }
    
    BaseBundle() {
        this((ClassLoader) null, 0);
    }
    
    public void putString(@Nullable String key, @Nullable String value) {
        unparcel();
        mMap.put(key, value);
    }


版本兼容

有时候在高版本上开发时到兼容低版本的手机上,就需要对版本进行检测


public void btnOnclick(){
        int cx = btn_open.getWidth()/2;
        int cy = btn_open.getHeight()/2;

        float radius = btn_open.getWidth();
        //使用5.0的特性,这里先做检查,如果不做检查的话,在低版本上运行会崩溃。
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            Animator anim = ViewAnimationUtils.createCircularReveal(btn_open,cx,cy,radius,0);
            anim.addListener(new AnimatorListenerAdapter() {

            });
        }
}

通过检查版本号基本上程序就可以运行了。如果设置完项目还提示错误的话就要使用下面的标签。以前在eclipse上使用lint会提示错误,但是现在使用studio时没有提示,还不知道什么原因。

@TargetApi(21)

告诉编译器在低于版本号21的机器上不要提示错误。

@SuppressLint("NewApi")

如果api版本高于最低版本,则忽略错误提示

相关文章

网友评论

      本文标题:Activity

      本文链接:https://www.haomeiwen.com/subject/utsdrttx.html