关闭

Android开发杂记

334人阅读 评论(0) 收藏 举报

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2447次
    • 积分:55
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档