Android->Activity 学习

来源:互联网 发布:java excel文件预览 编辑:程序博客网 时间:2024/06/09 21:34

1.生命周期
onCreate()->onStart()->onResume()->onPause()->onStop()->onDestory()

onStop()->onRestart()->onStart()// 一般不使用

2.多个Activity交互
A.onCreate()->A.onStart()->A.onResume()->A.onPause()->B.onCreate()->B.onStart()->B.onResume()->A.onStop()

点击Back键时:
开启新的Activity时先调用自己的onPause()方法,然后调用新Activity的onCreate()->onStart()->onResume(),最后调用自己的onStop()方法。

3.横竖屏切换
常规:onPause()->onStop()->onDestory()->onCreate()->onStart()->onResume()
使用onSaveInstanceStart(Bundle outState)来保存界面的一些信息:
在onCreate()方法中的saveInstanceState来获取信息.

4.启动方式
1)直接启动
I. Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);

    II. Intent intent = new Intent();        Component component = new Component(MainActivity.this,SecondActivity.class);        intent.setCompontent(component);        startActivity(intent);2)匿名启动    在AndroidManifest.xml文件中配置:    <activity>        <intent-filter>            <action android:name="mooc"/>            <category android:name="android.intent.category.DEFAULT/>        </intent-filter>    </activity>    在需要开启Activity的位置写以下代码    Intent intent = new Intent();    intent.setAction("mooc");    startActivity(intent);

5.打开系统常见的Activity
I. 系统浏览器:
ACTION–ACTION_VIEW
Uri uri = Uri.parse(“http://www.baidu.com/“);
intent.setData(uri);
II. 系统图库:
ACTION–ACTION_GET_CONTENT
intent.setType(“image/”);
III.发送短信:
ACTION–ACTION_SEND
intent.setType(“text/plain”);
intent.putExtra(Intent.EXTRA_TEXT,”I am a boy!”);
IV. 启动电话界面
ACTION–ACTION_CALL
Uri uri = Uri.parse(“tel:123456”);
intent.setData(url);

6.数据交互
I.
A界面:
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra(“key”,”value”);
startActivity(intent);

    B界面:    Intent intent = getIntent();    if(intent!=null){        String value = intent.getStringExtra("key");    }II.    A界面:    Intent intent = new Intent(MainActivity.this,SecondActivity.class);    Bundle bundle = new Bundle();    bundle.putString("key","value");    intent.putExtras(bundle);    startActivity(intent);    B界面:    Intent intent = getIntent();    if(intent!=null){        String value = intent.getStringExtra("key");    }III.传递对象    A界面:    Intent intent = new Intent(MainActivity.this,SecondActivity.class);    // Person类需要实现Serializable    Person person = new Person();    Bundle bundle = new Bundle();    bundle.putSerializable("person",person);    intent.putExtras(bundle);    startActivity(intent);    B界面:    Intent intent = getIntent();    if(intent!=null){        Person pseron = intent.getSerializableExtra("person");    }III.传递一张图片    A界面:    Intent intent = new Intent(MainActivity.this,SecondActivity.class);    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);    Bundle bundle = new Bundle();    bundle.putParcelable("bitmap",bitmap);    intent.putExtras(bundle);    startActivity(intent);    B界面:    Intent intent = getIntent();    if(intent!=null){        Bitmap bitmap = intent.getParcelableExtra("bitmap");    }其中Bundle对象传递数据应该小于0.5M的数据,传递大数据时将无法开启新的Activity。

7.启动模式
I. Back Stack(后台任务栈)–特点:后进先出
获得任务ID: getTaskId();
II. android:launcher->
standard/singleTop/singleTask/singleInstance

——-深入了解Activity
1.与Activity相关的Framework层类介绍
1) ActivityThread
I. 本身不是一个线程
II. 在主线程的方法当中运行
Looper.prepareMainLooper();
ActivityThread thread= new ActivityThread();
thread.attach(false);
Looper.loop();
III. ActivityThread启动完成后通知AMS。
IAcitivityManager mgr = ActivityManagerNative.getDefault();
mgr.attachApplication(mAppThread);
2) ApplicationThread
3) ActivityClientRecord
4) ActivityRecord
5) Context–ContextImpl–ContextWrapper

2.生命周期具体回调
1) Instrumentation: Application的创建生命周期的管理,启动Activity.
2) ActivityManagerNative: 远程代理
3) onCreate(),onResume()

3.onDestroy()并未真的销毁Activity。

原创粉丝点击