Android学习笔记 活动大类

1.活动需要在AndroidManifest中注册<注册声明要放在 application标签内>

2.Toast.makeText(要显示的活动,要显示的语句,Toast.LENGTH_SHORT/LONG).show();显示相应提醒方式

3.活动启动直接调用onCreate方法

    先super.onCreate(savedInstanceState);

   然后 setContentView(活动对应的layout);

4.使用显示Intent

    Intent intent = new Intent(上下文,目标活动);

    startActivity(intent);

  使用隐式Intent

   设置中的和属性

   Intent intent = new Intent(这里填入 action的值);

   Intent.addCategory(这里填入category的值);

   startActivity(intent);

5.向下一个活动传递数据

    String date=“XXX”;

    Intent intent = new Intent(上下文,目标活动);

    Intent.putExtra(“定义的名字”,date);

    startActivity(intent);

在目标活动中

    Intent intent = getIntent();

    String date = intent.getStringExtra(“定义的名字”);

6.返回数据给上一个活动

    startActivityForResult(intent,请求码)来启动活动

    在OnActivityResult(请求码,活动返回数据处理结果,data)方法中

    switch(请求码);

               Case请求码

                         if(resultCode ==活动返回数据处理结果)

                         String date = date.getStringExtra(“定义的名字”);//成功获取下一个活动传过来的数据

    在目标活动中

    Intent intent = new Intent();

    Intent.putExtra(“定义的名字”,”数据”);

   setResult(用于向上一个活动返回处理结果,intent);

   finish;

7.活动生命周期

    4种状态:运行状态(活动处于栈顶)、暂停状态(活动不处于栈顶 但任然可见)、停止状态(活动不处于栈顶且完全不可见)、销毁状态

    onCreate -onStart- onResume- onPause- onStop- onDestory(onRestart)

8.活动被回收怎么办

   onSaveInstanceState(Bundle outState)方法临时保存数据

   outState.putString(“定义的名字”,”数据”);


   在onCreate方法中

   判断 savedInstanceState 是不是为空 

   String date = savedInstanceState.getString(“定义的名字”);

9.活动的四种启动模式

需要在标签中声明启动模式

例如:android:launchMode=“singleTop”

Standard (默认启动模式)

singleTop

singleTask(看栈里面有没有,如果有 直接把它之上的所有活动出栈)

singleInstance:启用一个新的返回栈来管理活动

10.如何清楚活动需要传递哪些数据

     在目标活动中 添加actionStart的静态方法

    actionStart(Context context,String data1,String date2)

    Intent intent = new Intent(context,目标活动);

    intent.putExtra(“定义的名字1”,data1);

    intent.putExtra(“定义的名字2”,data2);

    context.startActivity(intent);


   在上下文中

   目标活动.actionStart(上下文,”要传送的数据1”,”要传送的数据2”);//可以直接知道要传哪些数据以及数据的数据类型

你可能感兴趣的:(Android学习笔记 活动大类)