android - 通过getIntent()获得的Intent有什么特殊的吗?
伊谢尔伦
伊谢尔伦 2017-04-18 09:04:59
0
1
414

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

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

  • Intent intent1 = getIntent(); //此方法在MainActivity中调用

  • Intent intent2 = new Intent(MainActivity.this, MainActivity.class);

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

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
刘奇

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template