Android mobile application foundation tutorial [program activity]
•
Android
@H_ 301_ 155@
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent,1);
//使用startActivityForResult方法开启SecondActivity,第1个参数是Intent对象,第2个参数是请求码,用于标识请求的来源。
Intent intent = new Intent();
intent.putExtra("data","Hello MainActivity");
setResult(2,intent);//在SecondActivity 中添加返回数据。
finish();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {// SecondActivity被销毁之后在MainActivity中回调onActivityResult()方法。
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1&&resultCode == 2){
String acquiredData= data.getStringExtra("data"); //获取回传的数据
Toast.makeText(MainActivity.this,acquiredData,Toast.LENGTH_SHORT).show();
}
}
NewsListFragment fragment = new NewsListFragment();//实例化Fragment对象
FragmentManager fm = getFragmentManager();//获取FragmentManager实例
FragmentTransaction beginTransaction = fm.beginTransaction();//开启事务
beginTransaction.replace(R.id.ll,fragment); //添加Fragment
beginTransaction.commit();//提交事务
This chapter mainly introduces the relevant knowledge of activity, including the life cycle of activity, how to create, open and close a single activity, intent and intentfilter, jump and data transfer between activities, the startup mode of activity and the use of fragment. Activities and data transfer between activities are the most used in Android programs, so readers are required to master this part.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码