本文主要是介绍[深入frameworks]——startActivity的执行流程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于Android9.0的代码分析
平时我们从一个Activity
启动另外一个Activity
,只需要调用startActivity()
方法就可以实现,那么实际上是怎样实现的呢?为什么调用startActivity
就可以打开另外一个Activity
呢?在本文中我们就一起来探讨一下。
在对调用过程的分析中,我也附上了一些调用过程的调试信息,希望能够帮助读者更加清晰的了解这个过程。
1 开始调用context.startActivity
这里我就以stratActivity(intent)
为例进行说明。先看下Activity
中这个方法的具体实现:
1.1 Activity.java -> startActivity(intent)
/*** Same as {@link #startActivity(Intent, Bundle)} with no options* specified.** @param intent The intent to start.** @throws android.content.ActivityNotFoundException** @see #startActivity(Intent, Bundle)* @see #startActivityForResult*/
@Override
public void startActivity(Intent intent) {this.startActivity(intent, null);
}
首先在FirstActivity
调用startActivity
去启动SecondActivity
。
1.1.1 相应的调用调试图
1.1.2 调试图分析
从上图可以看到,startActivity
的参数intent
,它的component
其实是com.example.activitytest/.SecondActivity
,并且还携带参数(has extras
)。
并且startActivity(intent)
的实现其实是调用它的一个重载方法startActivity(intent, options)
,调用真正实现逻辑的方法,将不用的参数传入null
,这种实现方式在Java中很常见,比如类的构造方法,有无参的,一个乃至多个参数的构造方法,往往其具体实际实现是在多个参数的构造方法中,至于其他较少参数的构造方法,则是将多出来的参数传入无效值。
1.2 startActivity(intent, options)
/*** Launch a new activity. You will not receive any information about when* the activity exits. This implementation overrides the base version,* providing information about* the activity performing the launch. Because of this additional* information, the {@link Intent#FLAG_ACTIVITY_NEW_TASK} launch flag is not* required; if not specified, the new activity will be added to the* task of the caller.** <p>This method throws {@link android.content.ActivityNotFoundException}* if there was no Activity found to run the given Intent.** @param intent The intent to start.* @param options Additional options for how the Activity should be started.* See {@link android.content.Context#startActivity(Intent, Bundle)}* Context.startActivity(Intent, Bundle)} for more details.** @throws android.content.ActivityNotFoundException** @see #startActivity(Intent)* @see #startActivityForResult*/
@Override
public void startActivity(Intent intent, @Nullable Bundle options) {if (options != null) {startActivityForResult(intent, -1, options);} else {// Note we want to go through this call for compatibility with// applications that may have overridden the method.startActivityForResult(intent, -1);}
}
1.2.1 相应的调用调试图
1.2.2 调试图分析
传入的intent不变,而options传入为null,在这个方法中有对options无效的具体判断,并且调用startActivityForResult(intent, requestCode)
。
1.3 startActivityForResult(intent, requestCode)
/*** Same as calling {@link #startActivityForResult(Intent, int, Bundle)}* with no options.** @param intent The intent to start.* @param requestCode If >= 0, this code will be returned in* onActivityResult() when the activity exits.** @throws android.content.ActivityNotFoundException** @see #startActivity*/
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {startActivityForResult(intent, requestCode, null);
}
1.3.1 相应的调用调试图
1.3.2 调试图分析
继续调用startActivityForResult(intent, requestCode, options)
,requestCode
和options
仍然掺入无效值。
1.4 startActivityForResult(intent, requestCode, options)
/*** Launch an activity for which you would like a result when it finished.* When this activity exits, your* onActivityResult() method will be called with the given requestCode.* Using a negative requestCode is the same as calling* {@link #startActivity} (the activity is not launched as a sub-activity).** <p>Note that this method should only be used with Intent protocols* that are defined to return a result. In other protocols (such as* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may* not get the result when you expect. For example, if the activity you* are launching uses {@link Intent#FLAG_ACTIVITY_NEW_TASK}, it will not* run in your task and thus you will immediately receive a cancel result.** <p>As a special case, if you call startActivityForResult() with a requestCode* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your* activity, then your window will not be displayed until a result is* returned back from the started activity. This is to avoid visible* flickering when redirecting to another activity.** <p>This method throws {@link android.content.ActivityNotFoundException}* if there was no Activity found to run the given Intent.** @param intent The intent to start.* @param requestCode If >= 0, this code will be returned in* onActivityResult() when the activity exits.* @param options Additional options for how the Activity should be started.* See {@link android.content.Context#startActivity(Intent, Bundle)}* Context.startActivity(Intent, Bundle)} for more details.** @throws android.content.ActivityNotFoundException** @see #startActivity*/
public
这篇关于[深入frameworks]——startActivity的执行流程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!