gpt4 book ai didi

android - 从服务调用时 Activity 重新启动

转载 作者:行者123 更新时间:2023-11-29 23:33:39 25 4
gpt4 key购买 nike

我的应用程序中有一个 float 按钮服务(比如 facebook messenger)单击时,我的 Activity 从后台返回,但返回方式与缩小时不同,它再次开始调用 onCreate,知道是什么原因导致此行为吗?

在我的 list 中, Activity 与属性有关:

            android:launchMode="singleTask"

它被我的服务这样调用:

    Intent i = new Intent(FloatingViewService.this, DrawerActivity2.class);
i.putExtra("fromController",true);
i.setFlags(FLAG_FROM_BACKGROUND);
FloatingViewService.this.startActivity(i);

我的主要问题是在我有一个 webview 的 Activity 中,每次单击按钮并调用 Activity 时它都会重新加载,而不是仅仅保持相同的方式。

更新后这是我在 list 中的 Activity

        <activity
android:name=".DrawerActivity2"
android:configChanges="orientation"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/OtherTheme"/>

更新 2

值得一提的是,它是一个位于应用程序中的库。

我试过这个代码来打开 Activity :

Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); startActivity(intent);

它打开了第一个(原始应用程序的) Activity ,而不是打开的那个。

我们将不胜感激

最佳答案

我只能想象你的 FLAG_FROM_BACKGROUND 是什么意思。我想知道它是不是这样调用的,因为你之前有过一些崩溃,比如“Activity 可以在没有来自 applicationContext bla bla 的 FLAG_NEW_TASK 的情况下启动”。随便。

因此,如果您有 Intent.FLAG_ACTIVITY_NEW_TASK,您将始终在 NEW_TASK 中实例化您的 DrawerActivity2。它将永远是,你知道“新的” Activity 。将 launchMode 更改为 singleTop 不会产生预期的结果。 Read about launchModes and Flags here

请注意,您不能在具有 ApplicationContext(或 BaseContext/ContextWrapper Service 扩展)。为此,您必须使用 Activity 的上下文开始新的 Activity

关于 Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());开始 Activity ( Intent );可能它按预期工作

The current implementation looks first for a main activity in the category Intent.CATEGORY_INFO, and next for a main activity in the category Intent.CATEGORY_LAUNCHER. Returns null if neither are found.

Android developers source

然后你问

my activity returns from the background, it starts over calling onCreate once more , any idea what causes this behavior ?

我的假设是FLAG_FROM_BACKGROUND:

Intent i = new Intent(FloatingViewService.this, DrawerActivity2.class);
i.putExtra("fromController",true);
i.setFlags(FLAG_FROM_BACKGROUND); /*is it really Intent.FLAG_ACTIVITY_NEW_TASK ???*/
FloatingViewService.this.startActivity(i);

如何解决?可能使用 ActivityContext 而不是 ApplicationContext 来启动 Activity。但这是另一回事。并且不能推荐一种“好的”方法来实现它。

关于android - 从服务调用时 Activity 重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52465923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com