我有一个应用程序,它有 2 个活动。
<activity android:name=".LauncherActivity"
android:theme="@style/LauncherTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"/>
1.(我期望)
通过命令行安装应用程序(adb install -r app)。
点击应用图标打开它,LauncherActivity显示,然后我startActivity到MainActivity,MainActivity显示。
点击 HOME,然后再次点击应用程序图标,MainActivity 再次显示。
2.(例外?)
通过packageinstaller安装应用程序。
安装完成后,点击 packageinstaller 中的“Open”按钮,LauncherActivity 显示,然后我 startActivity 到 MainActivity,MainActivity 显示。
点击 HOME,然后再次点击应用程序图标,LauncherActivity 再次显示!!
在我的 LauncherActivity
private void startMainActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
我调查了源,packageinstaller 使用 mLaunchIntent 启动活动
安装AppProgress.java
mLaunchIntent = getPackageManager().getLaunchIntentForPackage(mAppInfo.packageName);
应用程序包管理器.java
@Override
public Intent getLaunchIntentForPackage(String packageName) {
// First see if the package has an INFO activity; the existence of
// such an activity is implied to be the desired front-door for the
// overall package (such as if it has multiple launcher entries).
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_INFO);
intentToResolve.setPackage(packageName);
List<ResolveInfo> ris = queryIntentActivities(intentToResolve, 0);
// Otherwise, try to find a main launcher activity.
if (ris == null || ris.size() <= 0) {
// reuse the intent instance
intentToResolve.removeCategory(Intent.CATEGORY_INFO);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage(packageName);
ris = queryIntentActivities(intentToResolve, 0);
}
if (ris == null || ris.size() <= 0) {
return null;
}
Intent intent = new Intent(intentToResolve);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(ris.get(0).activityInfo.packageName, ris.get(0).activityInfo.name);
return intent;
}
我没有信息活动,所以意图是:
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage(packageName);
我很困惑。为什么会有不同的行为?帮助!