Activity的生命周期(五)——Activity生命周期的应用场景
2016-03-05 20:28
603 查看
通过音乐的播放、暂停、继续播放来表现Activity生命周期的应用
activity_many_a.xml
activity_transvalue_b.xml
public class UseActivityLife extends Activity implements OnClickListener{ private String TAG = "UseActivityLife"; private MediaPlayer music; private int positon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_many_a); Button btn_switchToB = (Button) findViewById(R.id.btn_switchToB); btn_switchToB.setOnClickListener(this); Log.e(TAG, "onCreate"); music = MediaPlayer.create(this, R.raw.beyond); music.start(); } @Override protected void onStart() { super.onStart(); Log.e(TAG, "onStart"); } @Override protected void onRestart() { super.onRestart(); Log.e(TAG, "onRestart"); } @Override protected void onResume() { super.onResume(); Log.e(TAG, "onResume"); if(positon != 0){ music.seekTo(positon); music.start(); } } @Override protected void onPause() { super.onPause(); Log.e(TAG, "onPause"); if(music.isPlaying()){ music.pause(); positon = music.getCurrentPosition(); } } @Override protected void onStop() { super.onStop(); Log.e(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.e(TAG, "onDestroy"); if(music != null){ music.release(); music = null; } } @Override public void onClick(View v) { startActivity(new Intent(UseActivityLife.this, ManyActivity_B.class)); } }
public class ActivityTransValue_B extends Activity{ private TextView tv_value; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_transvalue_b); tv_value = (TextView) findViewById(R.id.tv_value); Intent intent = getIntent(); if(intent != null){ String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age",0); tv_value.setText("intent.putExtra传值: "+"name:"+name+"——"+"age:"+age); } } }
activity_many_a.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.imooc.activitystudy.OneActivity" > <TextView android:id="@+id/tv_act" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Activity_A" /> <Button android:layout_below="@id/tv_act" android:id="@+id/btn_switchToB" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="跳转到Activity_B" /> </RelativeLayout>
activity_transvalue_b.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.imooc.activitystudy.OneActivity" > <TextView android:id="@+id/tv_value" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ImageView android:layout_below="@id/tv_value" android:id="@+id/im_value" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
相关文章推荐
- boolean和它的包装类的区别
- 文章标题
- c++ 引用
- [国嵌攻略][107][Linux进程管理子系统]
- egret学习记录
- POJ-1230 Pass-Muraille
- 把几个降维的算法(FA PCA SVD ICA LPP LDA )归纳一下
- 两种框架之间的比较 Hibernate与Mybatis
- 如何使用Git客户端进行文件(代码)上传
- Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
- linux内核分析--操作系统是如何工作的?
- javaweb笔记全套
- usaco 1.1.4 beads
- linux network namespace 学习
- apt-get update 失败
- Android 解决异步任务下载图片错位问题
- 杭电1202,此题个人觉得有误
- 对二进制加密(分散保存-s=sy+a+b)
- target文件夹里的资源无法被程序加载
- 博弈(alpha-beta 剪枝)POJ —— 1085 Triangle War