Android开发杂记

1、如果在一个activity A的onCreate()或者onResume()方法中调用方法startActivity()启动另一个activity B,那么activity B会在activity A的onResume方法结束后才启动,为了防止启动activity B时出现画面闪烁,activity A的window不会显示。

参考Activity.java的startActivityForResult()方法的注释。

    /**
     * Launch an activity for which you would like a result when it finished.
     * When this activity exits, your
     * onActivityResult() method will be called with the given requestCode. 
     * Using a negative requestCode is the same as calling 
     * {@link #startActivity} (the activity is not launched as a sub-activity).
     *
     * <p>Note that this method should only be used with Intent protocols
     * that are defined to return a result.  In other protocols (such as
     * {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
     * not get the result when you expect.  For example, if the activity you
     * are launching uses the singleTask launch mode, it will not run in your
     * task and thus you will immediately receive a cancel result.
     *
     * <p>As a special case, if you call startActivityForResult() with a requestCode 
     * >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
     * activity, then your window will not be displayed until a result is 
     * returned back from the started activity.  This is to avoid visible 
     * flickering when redirecting to another activity. 
     *
     * <p>This method throws {@link android.content.ActivityNotFoundException}
     * if there was no Activity found to run the given Intent.
     *
     * @param intent The intent to start.
     * @param requestCode If >= 0, this code will be returned in
     *                    onActivityResult() when the activity exits.
     * @param options Additional options for how the Activity should be started.
     * See {@link android.content.Context#startActivity(Intent, Bundle)
     * Context.startActivity(Intent, Bundle)} for more details.
     *
     * @throws android.content.ActivityNotFoundException
     *
     * @see #startActivity 
     */
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        ...//代码详见 Activity.java
  }

2、layout A使用<include >包含另外一个layout B时,覆盖layout B中的属性满足两个规则:

1)<include >的android:id属性会覆盖layout B的android:id属性;

2)<include >只能覆盖layout B中的android:layout_相关的属性,并且至少覆盖android:layout_widthandroid:layout_height属性,否则<include >中包含的layout相关的属性会被忽略。

3、在Activiy被销毁(主动调用finish方法或者被系统销毁)时,在Activity中创建的Thread并不会被一起销毁,特别是带有循环的thread,为了防止内存泄露,应该手动销毁thread。

public class DemoActivity extends Activity {
    ...
    
    //线程结束标志
    boolean stopThread = false;

    ...
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ...
        
        // 创建新线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "thread is Runing....");
                while (!stopThread) {
                    // do something
                }
                Log.d(TAG, "thread is over....");
            }
        }).start();
        
        ...
    }

    ...
    
    @Override
    protected void onDestroy() {
        // 置位线程结束标志
        stopThread = true;
        super.onDestroy();
    };
    
    ...
    
}

4、Android的Task是完成一项具体的工作所涉及的Activity(可能来自不同的Application)的集合,这些Activity按被启动的顺序放在一个Back Stack中。

“A task is a collection of activities that users interact withwhen performing a certain job. The activities are arranged in a stack (the "back stack"), in theorder in which each activity is opened.” Tasks and Back Stack

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值