Anthology of Android Development Engineers - Activity life cycle, startup method, Intent related introduction, Activity detailed explanation

foreword

Hello everyone, this is my  overview Victoday , I hope you like itAndroid开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解

what is activity

As an Activity, it is an interface. When we open an APP on the mobile phone, the page you see is generated based on the Activity.

Then when you click a button to jump to another interface, it is another Activity interface, and Activity can separate a lot of knowledge points.

For example, the life cycle of the activity, the way the activity is started, and if the data of the two activities interacts, that is, the data interaction between the page and the page. For example, the data you enter on another interface is saved to another page. , to display the effect; knowledge about the startup of the activity.

activity lifecycle logic

First call the onCreate() method to create an Activity, then call the onStart() method and the onResume() method, we can see the created interface.
Such as: activity -> onCreate()-onStart()-onResume()
Only after calling these three methods can you see that the page is the activity.

@Override
protected void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
}
@Override
protected void onStart(){
 super.onStart();
}
@Override
protected void onResume(){
 super.onResume();
}
@Override
protected void onRestart(){
 super.onRestart();
}
@Override
protected void onPause(){
 super.onPause();
}
@Override
protected void onStop(){
 super.onStop();
}
@Override
protected void onDestroy(){
 super.onDestroy();
}

If you click back, it means that there is a back button on the phone to go back, then those methods will be activated. Answer, the onPause()->onStop()->onDestroy() method will be activated. Then the interface you open will return to the previous effect when you opened the interface, which is to return to the effect of the mobile phone page you did not click on the APP.

整个启动到退回(从创建到销毁):
onCreate()->Created->onStart()->Started->onResume()->Resumed->onPause()->Paused->onStop()->Stopped->onDestroy()->Destroyed

onCreate()->onStart()->onResume()->onPause()->onStop()->onDestory()

  • 可见状态:onCreate()->onStart()->onResume()
  • 不可见状态:onPause()->onStop()
  • 销毁状态:onDestory()

从一个页面跳转到另一个页面

两个activity进行交互,周期是怎么样的呢,交互就是一个页面跳转到另一个页面效果。

调用了哪些方法:
MainActivity onCreate()->MainActivity onStart()->MainActivity onResume()->MainActivity() onPause()->SecondActivity onCreate()->SecondActivity onStart()->SecondActivity onReume()->MainActivity onStop()

这样更看得懂了哦~
MainActivity onCreate()->MainActivity onStart()->MainActivity onResume()
MainActivity() onPause()
SecondActivity onCreate()->SecondActivity onStart()->SecondActivity onReume()
MainActivity onStop()

如果显示第二个页面,再点击back,那么就会回到第一个界面了。

那么这个过程调用了什么方法:
SecondActivity onPause()->MainActivity onRestart()->MainActivity onStart()->MainActivity onResume()->SecondActivity onStop()->SecondActivity onDestroy()

这样更看得懂了哦~
MainActivity onCreate()->MainActivity onStart()->MainActivity onResume()
MainActivity() onPause()
SecondActivity onCreate()->SecondActivity onStart()->SecondActivity onReume()
MainActivity onStop()
SecondActivity onPause()
MainActivity onRestart()->MainActivity onStart()->MainActivity onResume()
SecondActivity onStop()->SecondActivity onDestroy()

在这里你会注意到 onPause() 这个方法,当跳转到别的界面的时候,会先调用onPause()的方法,MainActivity跳转到SecondActivity时会先调用 MainActivity onPause()的方法,那么SecondActivity跳转到MainActivity的时候回用 SecondActivity onPause()的方法。

问:如果在这里你会问 onRestart()的这个方法,你这个靠英语理解一下就可知道的,重新启动,那么会问为什么不onCreate()的方法,因为MainActivity()的这个已经创建了,所以就不用而用onRestart()的方法。

onPause()这个方法是代表停止状态,如果第一个界面跳转另一个界面,先停止第一个界面的状态,这样另一个界面的打开,就会保证了没有第一个界面的如何效果,因为被暂停了嘛。

MainActivity onStop()放在 SecondActivity onCreate() 之前会导致什么呢,会出现闪退效果的。onStop()调用,后台运行,不会销毁。

竖屏与横屏周期

什么是竖屏与横屏周期,就是手机上有横屏看,和竖屏看的效果,周期也是有不一样的。

调用的方法,由竖屏变化横屏:
onCreate()->onStart()->onResume()->onPause()->onStop()->onDestory()
onCreate()->onStart()->onResume()

竖屏与横屏切换会闪屏效果,这是创建到销毁到创建的效果。

Activity的启动方式

一种是直接,一种是匿名。不同的启动方式有什么不同的效果呢。

直接启动方式:

Intent intent = new Intent(MainActivity.this,SecondActivity.class):
startActivity(intent);

当使用隐式时,我们需要知道如何处理它。
setComponent(ComponentName)

跳转浏览器

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri url = Uril.parse("http://jianshu.com");
intent.setData(url);
startActivity(intent);

跳转相册

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivity(intent);

发送信息

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT,"I am a vic"):
startActivity(intent);

打开电话

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri url=Uri.parse("tel:123456789");
intent.setData(url):
startActivity(intent);

Intent相关介绍

Intent是要执行的操作的抽象描述。

ACTION_VIEW:显示关于标识符为“1”的人的信息
tel:123 - 显示填入给定号码的电话拨号程序
type :数据的显式类型
ACTION_ANSWER:处理来电
ACTION_BATTERY_LOW:设备电池电量不足
ACTION_GET_CONTENT:允许用户选择特定类型的数据并将其返回
ACTION_HEADSET_PLUG:插入有线耳机或拔下插头
setData:设置数据
setAction :设置要执行的一般操作等。

如果觉得不错,那就点个赞吧!❤️

总结

  • 本文讲了Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解,如果您还有更好地理解,欢迎沟通
  • 定位:分享 Android&Java知识点,有兴趣可以继续关注

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324530672&siteId=291194637