Android开发从0开始(Activity篇)

Activity的生命周期

对应解释:

startActivity(new Intent(源页面.this,目标页面.class))    结束当前活动页面finish();

Activity的启动模式

  App先后打开两个活动,此时活动会放入栈内。

(Android:launchMode=standard)默认

(Android:launchMode=singleTop)栈顶复用(渠道多)

(Android:launchMode=singleTask)栈内复用(主界面)

(Android:launchMode=singleInstance)全局唯一模式。

动态设置模式

Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)设置启动标识,几十存在待跳转活动实例,清除上方所有实例,重新创建实例,保证栈中只有该活动的唯一实例。

Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)清空活动栈的所有实例。所以也要同时启动新任务栈   (Intent.FLAG_ACTIVITY_NEW_TASK)。场景例如登录页面。获取验证。

Intent各个组件之间数据传输

显示Intent:

①在Intent的构造函数中指定。

Intent intent= new Intent(FristActivity.this,second_layout.class);//创建指定目标明确的意图

②调用意图对象的setClass方法。

Intent intent =new Intent( ); //创建一个意图

Intent.setClass (this, ActNextActivity.class);//set设置意图跳转

③调用意图对象的setComponent方法。

Intent intent =new Intent( ); //创建一个意图

//创建包含目标活动在内的意图组件对象(可传类名,包名)

ComponentName componet = new ComponentName(this,ActNextActivity.class);

Intent.setCompent(component); //设置意图携带组件信息

隐士Intent:(没指定要跳转的活动目标,只给出一个动作字符串,系统自动匹配)

可通过setAction方法指定,或者构造函数Intent(STring action)常见系统动作。

示例:

Intent intent= new Intent();

//设置动作意图为准备拨号

intent.setAction(Intent.ACTION_DIAL);    

Uri uri= Uri.parse("tel"+phone);

intent.setData(uri);

startActivity(intent);

 其他隐intent使用:

向Activity发送数据:

向下一个activity传输数据

 Intent使用Bundle对象存放传递的数据信息。 Bundle对象读getExtras,写putExtras。

发                                        Bundle:是map数据结构     

 Intent intent = new Intent(this,Receiveactivity.class);

        Bundle bundle= new Bundle();

        bundle.putString("request_content",tv_send.getText().toString());

        intent.putExtras(bundle);

        startActivity(intent);

        setContentView(R.layout.activity_receiveactivity);

        tv_receive= findViewById(R.id.tv_receive);

        //从上面一个页面传来意图中获取快递包裹

        Bundle bundle= getIntent().getExtras();

        String request_content=bundle.getString("request_content");

        String desc= String.format("收到请求内容: %s",request_content);

        tv_receive.setText(desc);

向上一个activity返回数据

 下一个页面打包应答数据调用setResult方法返回数据包,上一个页面重写onActivityResult解析返回数据。

为activity补充附加信息

  ①利用资源文件配置字符串

    //从string.xml中获取名叫weather_str的值

    String value= getString(R.string.weather_str);

    tv_resource.setText(value);

②利用元数据传递配置信息

在AndroidManifest.xml中<meta-data android:name="weather" android:value="晴天"/>。

(适合在第三方SDk,时候整合别人的源数据时候在全局配置里使用)

方法:调用getPackageManager方法获得当前活动的信息对象

调用包管理器的getActivityInfo方法获得当前活动下信息对象

  活动信息对象的metaData是Bundle包裹类型,调用包裹对象的getString获得参数            ActivityInfo info = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);

Bundle bundle = info.metaData;   //获取活动附加的元数据

  String weather =bundle.getString("weather");  //从bundle中获取.weather属性

③给页面注册快捷方式(长按app触发功能)

<?xml version="1.0" encoding="utf-8"?>

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

  <shortcut

      android:shortcutId="first"

      android:enable="true"

      android:icon="@mipmap/ic_launcher"

      android:shortcutShortLabel="first"

      android:shortcutLongLable="启停活动">

      <intent

          android:action="android.intent.action.VIEW"

          android:targetClass="android.app.Activity"

          android:targetPackage="com.example.test_application"

          />

      <categories android:name="android.shortcut.conversation"/>

  </shortcut>

</shortcuts>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值