Android学习之路——Activity(2)
来源:互联网 发布:池州学院大数据学院 编辑:程序博客网 时间:2024/06/02 04:24
本来打算一次性把Activity的所有学习内容记下来的昨天时间不够了,好了,闲话就不谈了,进入正题。
Activity的数据传递
如何在两个Activity之间传递数据呢?有两种办法。
第一种:
在一个HelloWord项目中,在包里面创建一个Aty1类,继承自Activity,并且在AndroidManifest.xml文件中注册这个Activity。代码如下:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Aty1"></activity> // 注册 </application>
并且在Aty1里面重写onCreate()。在activity_main.xml中添加一个按钮:
<Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="41dp" android:text="启动Activity" />
下面就是对MainActivity的操作了:
private Button btnStart; private String message = "Hello"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStart = (Button) findViewById(R.id.btnStart); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this,Aty1.class); i.putExtra("message",message); startActivity(i); //启动Aty1 } }); }
接下来就是接受信息了,首先创建一个xml文件,对应Aty.class,详情:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>
Aty1接受消息:
public class Aty1 extends Activity{ private TextView tvShow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.aty1); tvShow = (TextView) findViewById(R.id.tvShow); Intent intent = getIntent(); String message = intent.getStringExtra("message");//引号中为MainActivity传过来的变量名称 tvShow.setText(message); } }
第二种:使用bundle传输数据,主要用于大量数据传输。
还是刚才的两个Activity。MainActivity中:
Intent i = new Intent(MainActivity.this,Aty1.class);//创建一个Intent对象 Bundle bundle = new Bundle();//创建一个Bundle对象 bundle.putString("test", "Hello");//将数据放入上面的Bundle对象 i.putExtras(bundle);//将Bundle对象放入Intent对象中 startActivity(i);//启动
Aty1接受数据:
Bundle bundle = getIntent().getExtras(); String message= bundle.getString("message"); tvShow.setText(test);
调用另一个Activity并返回结果
1.final int CODE = 0x717;//定义一个请求码常量
2.startActivityForResult(intent,CODE);//启动新的Activity
3.Aty1.class中:
Intent data = new Intent();data.putExtra("result", "Hello MainActivity");setResult(code,data);//设置返回码,并返回调用该Activity的Activity//这里应该code的值设置为CODE的值finish();结束当前Activity
4.MainAtivity中重写onActivityResult方法
@Override protected void onActivityResult(int arg0, int arg1, Intent arg2) { String result = arg2.getStringExtra("result"); System.out.println(result); super.onActivityResult(arg0, arg1, arg2); }
小结:
在关闭Activity的时候,使用finish()方法有个地方要注意:如果当前的Activity不是主活动,那么执行finish()方法后,将返回到调用它的那个Activity;否则,将返回到主屏幕中。
注:当Activity从竖屏换到横屏的时候,Activity执行哪些方法?
答:onPause() onStop() onDestory() onCreate() onStart() onResume()
换回来一样的。
- Android学习之路——Activity(2)
- Android学习之路——Activity(1)
- Android学习之路——Android四大组件之activity(一)
- Android学习之路——Android四大组件之activity(二)数据的传递
- android 学习记录之——Activity
- Android菜鸟学习之路——Activity生命周期
- Activity -- Android学习之路
- Android学习之路2 Activity 之布局
- Android学习之路(六)之 Activity的生命周期
- Android学习之路(八)之 Activity数据返回
- Android学习之路之activity声明
- Android之Activity学习
- Android之Activity学习
- Android 学习之activity
- Android之Activity学习
- Android学习之Activity
- Android学习之Activity
- Android学习之Activity
- pthread_create函数编译时报错:undefined reference to 'pthread_create'
- JAVA --- 23 kinds of design patterns
- Memcache 文件系统MemcacheFS
- PHY常见问题说明
- 20 Best Code Review Tools for Developers
- Android学习之路——Activity(2)
- 2_opencv2计算机视觉学习_操作像素
- [theano] Install Theano on Windows 7 x64 using Cuda 7.0
- 电子邮件验证 正则表达式
- 2-4-2 学生类
- android与服务器通过socket通信
- 文件的上传
- Knights in Chessboard
- 使用eclipse开发STM32