Activity之间的跳转
显式跳转
在同一个应用中跳转,比如说从一个界面跳转到另一个界面
在两个界面只进行跳转不传数据
1.通过点击事件从第一个界面跳转到第二个页面
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN) {
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
return true
}
2.点击事件从第二个页面返回到第一个页面
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN){
finish()
}
return true
}
在两个界面之间互传数据进行跳转
1.点击事件后传递数据到第二个界面
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN){
val intent = Intent(this,SecondActivity::class.java)
val bundle = Bundle().apply {
putInt("number",1898)
putString("name","pharaoh")
}
intent.putExtra("friend",bundle)
startActivity(intent)
}
return true
}
2.第二个界面接受数据
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val bundle = intent.getBundleExtra("friend")
val name = bundle?.get("name")
val number = bundle?.get("number")
}
3.第二个界面点击事件后返回数据
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN){
Intent().apply {
putExtra("return","second返回的数据")
setResult(100,this)
finish()
}
}
return true
}
4.第一个界面接受数据
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN){
val intent = Intent(this,SecondActivity::class.java)
val bundle = Bundle().apply {
putInt("number",1898)
putString("name","pharaoh")
}
intent.putExtra("friend",bundle)
---------这里从startActivity改为 startActivityForResult------------
startActivityForResult(intent,200)
}
return true
}
override fun onActivityResult(requestCode: Int,
resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 200 && resultCode == 100){
val result = intent.getStringExtra("return")
Log.v("phah","$result")
}
}
隐式跳转
在不同的app之间进行跳转,比如说跳转到相机、支付宝、微信、拨打电话
应用之间的跳转
下面以点击按钮跳转到拨打电话界面为例
1.在manifests中配置user-permission
<uses-permission android:name="android.permission.CALL_PHONE"/>
2.向用户请求权限
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CALL_PHONE)!=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this, arrayOf(Manifest.permission.CALL_PHONE),1)
}
3.重写onActivityResult方法
override fun onActivityResult(requestCode: Int,
resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK){
}
4.控件实现setOnClickListener接口
binding.phone.setOnClickListener {
Intent().apply {
action = Intent.ACTION_DIAL
data = Uri.parse("tel:12341132100")
startActivity(this)
}
}
将自己的应用给外部使用同时传递数据
1.在自己的应用中的manifests配置
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="to"/>
</intent-filter>
2.外部想要跳转到自己的应用实现setOnClickListener并传递数据
binding.to.setOnClickListener {
Intent().apply {
action = Intent.ACTION_SEND
data = Uri.parse("to:自己写的app")
startActivity(this)
}
3.自己的应用接受数据
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val result = intent.dataString
Log.v("pharaoh","$result")
}
4.自己的应用返回数据给外部应用
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN){
Intent().apply {
putExtra("info","自己的app传给外部应用的数据")
setResult(10,this)
finish()
}
}
return true
}
5.外部应用接受数据
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.to.setOnClickListener {
Intent().apply {
action = Intent.ACTION_SEND
data = Uri.parse("to:自己写的app")
-----------改startActivity为startActivityForResult-----------------
startActivityForResult(this,888)
}
}
}
override fun onActivityResult(requestCode: Int,
resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == 10 && requestCode == 888){
val fromMyApp = data?.getStringExtra("info")
Log.v("pharaoh","$fromMyApp")
}
}