这让我难住了,我在Android 2.1-r8 SDK中使用这个:
ProgressDialog.show(getApplicationContext(), ....);
还有在
Toast t = Toast.makeText(getApplicationContext(),....);
使用getApplicationContext()同时崩溃ProgressDialog和Toast ....这让我想到了这个问题:
活动上下文和应用程序上下文之间的实际区别是什么,尽管都使用“上下文”这个词?
当你直接从主屏幕启动应用程序时,你可以看到这两种上下文之间的区别,当你的应用程序通过共享意图从另一个应用程序启动时。
下面是@CommonSenseCode提到的“非标准反向堆栈行为”的实际示例:
假设您有两个相互通信的应用程序,App1和App2。
从启动程序启动App2:MainActivity。然后从MainActivity启动App2:SecondaryActivity。在这里,无论是使用活动上下文还是应用程序上下文,两个活动都存在于同一个任务中,这是可以的(假设您使用了所有标准启动模式和意图标志)。你可以返回MainActivity,在最近的应用中,你只有一个任务。
假设现在您在App1中,并以共享意图(ACTION_SEND或ACTION_SEND_MULTIPLE)启动App2:MainActivity。然后从那里尝试启动App2:SecondaryActivity(总是使用所有标准启动模式和意图标志)。
结果是:
if you launch App2:SecondaryActivity with application context on Android < 10 you cannot launch all the activities in the same task. I have tried with android 7 and 8 and the SecondaryActivity is always launched in a new task (I guess is because App2:SecondaryActivity is launched with the App2 application context but you're coming from App1 and you didn't launch the App2 application directly. Maybe under the hood android recognize that and use FLAG_ACTIVITY_NEW_TASK). This can be good or bad depending on your needs, for my application was bad.
On Android 10 the app crashes with the message
"Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?".
So to make it work on Android 10 you have to use FALG_ACTIVITY_NEW_TASK and you cannot run all activities in the same task.
As you can see the behavior is different between android versions, weird.
if you launch App2:SecondaryActivity with activity context all goes well and you can run all the activities in the same task resulting in a linear backstack navigation.
我希望我添加了一些有用的信息
当你直接从主屏幕启动应用程序时,你可以看到这两种上下文之间的区别,当你的应用程序通过共享意图从另一个应用程序启动时。
下面是@CommonSenseCode提到的“非标准反向堆栈行为”的实际示例:
假设您有两个相互通信的应用程序,App1和App2。
从启动程序启动App2:MainActivity。然后从MainActivity启动App2:SecondaryActivity。在这里,无论是使用活动上下文还是应用程序上下文,两个活动都存在于同一个任务中,这是可以的(假设您使用了所有标准启动模式和意图标志)。你可以返回MainActivity,在最近的应用中,你只有一个任务。
假设现在您在App1中,并以共享意图(ACTION_SEND或ACTION_SEND_MULTIPLE)启动App2:MainActivity。然后从那里尝试启动App2:SecondaryActivity(总是使用所有标准启动模式和意图标志)。
结果是:
if you launch App2:SecondaryActivity with application context on Android < 10 you cannot launch all the activities in the same task. I have tried with android 7 and 8 and the SecondaryActivity is always launched in a new task (I guess is because App2:SecondaryActivity is launched with the App2 application context but you're coming from App1 and you didn't launch the App2 application directly. Maybe under the hood android recognize that and use FLAG_ACTIVITY_NEW_TASK). This can be good or bad depending on your needs, for my application was bad.
On Android 10 the app crashes with the message
"Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?".
So to make it work on Android 10 you have to use FALG_ACTIVITY_NEW_TASK and you cannot run all activities in the same task.
As you can see the behavior is different between android versions, weird.
if you launch App2:SecondaryActivity with activity context all goes well and you can run all the activities in the same task resulting in a linear backstack navigation.
我希望我添加了一些有用的信息