search

Home  >  Q&A  >  body text

android - 通过getIntent()获得的Intent有什么特殊的吗?

通过getIntent()获得的Intent用于开启Activity(Activity开启本身)时,观察发现Activity不会走onDestroy()方法.

那么以下两种获得Intent的方法在开启Activity时,有什么本质区别吗?

如果利用intent2startActivity() 那么会开启一个全新的Activity
如果用intent1 则不会.

看了下源码,但是由于本人太水,没有找到判断用于开启ActivityIntent是否是attach()Activity上的这个逻辑,所以比较困惑,望大神解答.

伊谢尔伦伊谢尔伦2772 days ago411

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-18 09:06:59

    The amount of information is too little, and the questioner did not post the key code: the declaration part of the Activity in AndroidManifest.xml. Then you can only rely on guessing.

    Looking at the content described by the subject and the behavior of Activity, the definition of Activity may be as follows:

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    If it is defined like this, and it is direct startActivity(getIntent()), 那肯定是不行的, 因为系统默认是以Launcher(应用入口)的行为来创建Intent的, 并且在已经启动应用的情况下, 是不会任何反应的(若已经切换到后台, 那么调用时就会将该应用切换回前台). 而startActivity(new Intent(...)), then there is no above problem.

    In addition, flags like onDestroy()并不是启动新Activity就会被调用的, 除非声明了android:noHistory="true"或者FLAG_ACTIVITY_CLEAR_TOP/FLAG_ACTIVITY_CLEAR_TASK.

    reply
    0
  • Cancelreply