bin*_*y37 5 android android-intent android-launcher android-homebutton
我有一个主要活动,其意图是 CATEGORY_HOME 表现得像一个启动器。它还具有 CATEGORY_LAUNCHER 以便用户可以从 App Drawer 访问。以下是主要活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
Run Code Online (Sandbox Code Playgroud)
我有另一个活动,其中包含一个按钮,可以使用 HOME 意图调用 MainActivity。它是 App Drawer 的另一个入口点。这是测试活动:
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
findViewById(R.id.btn_test).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(TestActivity.this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是 AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="nosensor"
android:resumeWhilePausing="true"
android:taskAffinity=""
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestActivity"
android:label="Test Launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Run Code Online (Sandbox Code Playgroud)
安装后,应用程序从应用程序抽屉中打开。然后按下主页按钮,启动器的多个选项出现。在选择这个时,再次出现 MainActivity。稍后,通过转到不同的启动器,从 App Drawer 打开测试活动。从那里,单击按钮以将 MainActivity 作为启动器打开。问题是即使 MainActivity 是 singleTask,它在两个不同的堆栈中也有条目。以下是获得的日志:
adb shell dumpsys 活动活动
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
Stack #1:
mFullscreen=true
mBounds=null
Task id #1352
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{90b88b8 #1352 I=com.example.binay.launcherdemo/.MainActivity U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a894 mCallingUid=u0a894 mUserSetupComplete=true mCallingPackage=com.example.binay.launcherdemo
intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.binay.launcherdemo/.MainActivity}
realActivity=com.example.binay.launcherdemo/.MainActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=0
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{b1b0736 u0 com.example.binay.launcherdemo/.MainActivity t1352}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/1352_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1532172058626 lastActiveTime=1532172058626 (inactive for 4s)
* Hist #0: ActivityRecord{b1b0736 u0 com.example.binay.launcherdemo/.MainActivity t1352}
packageName=com.example.binay.launcherdemo processName=com.example.binay.launcherdemo
launchedFromUid=10894 launchedFromPackage=com.example.binay.launcherdemo userId=0
app=ProcessRecord{3e17691 9030:com.example.binay.launcherdemo/u0a894}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10408000 cmp=com.example.binay.launcherdemo/.MainActivity }
frontOfTask=true task=TaskRecord{90b88b8 #1352 I=com.example.binay.launcherdemo/.MainActivity U=0 StackId=1 sz=1}
taskAffinity=null
realActivity=com.example.binay.launcherdemo/.MainActivity
baseDir=/data/app/com.example.binay.launcherdemo-1/base.apk
dataDir=/data/user/0/com.example.binay.launcherdemo
stateNotNeeded=true componentSpecified=true mActivityType=0
compat={320dpi} labelRes=0x7f0b0027 icon=0x7f0a0000 theme=0x7f0c0005
config={1.0 ?mcc?mnc [en_IN,ta_IN] ldltr sw360dp w360dp h616dp 320dpi nrml long port finger -keyb/v/h -nav/h s.6}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff3f51b5
launchFailed=false launchCount=1 lastLaunchTime=-4s516ms
haveState=false icicle=null
state=RESUMED stopped=false delayedResume=false finishing=false
keysPaused=false inHistory=true visible=true sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=2
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=true lastVisibleTime=-3s687ms
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #1358
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{e45d9f6 #1358 A=com.example.binay.launcherdemo U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a894 mCallingUid=u0a880 mUserSetupComplete=true mCallingPackage=com.apusapps.launcher
affinity=com.example.binay.launcherdemo
intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.binay.launcherdemo/.TestActivity bnds=[352,488][488,662]}
realActivity=com.example.binay.launcherdemo/.TestActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=0
rootWasReset=true mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{bcd0adc u0 com.example.binay.launcherdemo/.TestActivity t1358}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=android.graphics.Bitmap@66af1f7 lastThumbnailFile=/data/system_ce/0/recent_images/1358_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1532172058621 lastActiveTime=1532172058621 (inactive for 4s)
* Hist #0: ActivityRecord{bcd0adc u0 com.example.binay.launcherdemo/.TestActivity t1358}
packageName=com.example.binay.launcherdemo processName=com.example.binay.launcherdemo
launchedFromUid=10880 launchedFromPackage=com.apusapps.launcher userId=0
app=ProcessRecord{3e17691 9030:com.example.binay.launcherdemo/u0a894}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.binay.launcherdemo/.TestActivity bnds=[352,488][488,662] }
frontOfTask=true task=TaskRecord{e45d9f6 #1358 A=com.example.binay.launcherdemo U=0 StackId=1 sz=1}
taskAffinity=com.example.binay.launcherdemo
realActivity=com.example.binay.launcherdemo/.TestActivity
baseDir=/data/app/com.example.binay.launcherdemo-1/base.apk
dataDir=/data/user/0/com.example.binay.launcherdemo
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={320dpi} labelRes=0x0 icon=0x7f0a0000 theme=0x7f0c0005
config={1.0 ?mcc?mnc [en_IN,ta_IN] ldltr sw360dp w360dp h616dp 320dpi nrml long port finger -keyb/v/h -nav/h s.6}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff3f51b5
launchFailed=false launchCount=0 lastLaunchTime=-5s605ms
haveState=true icicle=Bundle[mParcelledData.dataSize=1064]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-5s221ms
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #1357
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{d4c7864 #1357 A=com.apusapps.launcher U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a880 mCallingUid=u0a880 mUserSetupComplete=true mCallingPackage=com.apusapps.launcher
affinity=com.apusapps.launcher
intent={act=android.intent.action.MAIN flg=0x18000000 cmp=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity}
realActivity=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{21fe202 u0 com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity t1357}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=android.graphics.Bitmap@795eacd lastThumbnailFile=/data/system_ce/0/recent_images/1357_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_UNRESIZEABLE isResizeable=false firstActiveTime=1532172057464 lastActiveTime=1532172057464 (inactive for 5s)
* Hist #0: ActivityRecord{21fe202 u0 com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity t1357}
packageName=com.apusapps.launcher processName=com.apusapps.launcher
launchedFromUid=10880 launchedFromPackage=com.apusapps.launcher userId=0
app=ProcessRecord{f590a8b 7920:com.apusapps.launcher/u0a880}
Intent { act=android.intent.action.MAIN flg=0x18000000 cmp=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity bnds=[288,1098][428,1280] (has extras) }
frontOfTask=true task=TaskRecord{d4c7864 #1357 A=com.apusapps.launcher U=0 StackId=1 sz=1}
taskAffinity=com.apusapps.launcher
realActivity=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity
baseDir=/data/app/com.apusapps.launcher-1/base.apk
dataDir=/data/user/0/com.apusapps.launcher
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={320dpi} labelRes=0x7f08005f icon=0x7f0200a3 theme=0x7f1101c1
config={1.0 ?mcc?mnc [en_IN,ta_IN] ldltr sw360dp w360dp h616dp 320dpi nrml long port finger -keyb/v/h -nav/h s.6}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff222222
launchFailed=false launchCount=0 lastLaunchTime=-11s467ms
haveState=true icicle=Bundle[mParcelledData.dataSize=3252]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=1
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-10s793ms
resizeMode=RESIZE_MODE_UNRESIZEABLE
Task id #1348
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{fed9f82 #1348 A=com.apusapps.launcher U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a880 mCallingUid=u0a880 mUserSetupComplete=true mCallingPackage=com.apusapps.launcher
affinity=com.apusapps.launcher
intent={act=android.intent.action.MAIN flg=0x18000000 cmp=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity}
realActivity=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{5f4526d u0 com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity t1348}]
askedCompatMode=false inRecents=false isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/1348_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_UNRESIZEABLE isResizeable=false firstActiveTime=1532172010058 lastActiveTime=1532172010058 (inactive for 53s)
* Hist #0: ActivityRecord{5f4526d u0 com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity t1348}
packageName=com.apusapps.launcher processName=com.apusapps.launcher
launchedFromUid=10880 launchedFromPackage=com.apusapps.launcher userId=0
app=ProcessRecord{f590a8b 7920:com.apusapps.launcher/u0a880}
Intent { act=android.intent.action.MAIN flg=0x18000000 cmp=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity bnds=[288,1098][428,1280] (has extras) }
frontOfTask=true task=TaskRecord{fed9f82 #1348 A=com.apusapps.launcher U=0 StackId=1 sz=1}
taskAffinity=com.apusapps.launcher
realActivity=com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity
baseDir=/data/app/com.apusapps.launcher-1/base.apk
dataDir=/data/user/0/com.apusapps.launcher
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={320dpi} labelRes=0x7f08005f icon=0x7f0200a3 theme=0x7f1101c1
config={1.0 ?mcc?mnc [en_IN,ta_IN] ldltr sw360dp w360dp h616dp 320dpi nrml long port finger -keyb/v/h -nav/h s.6}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff222222
launchFailed=false launchCount=0 lastLaunchTime=-2m24s974ms
haveState=true icicle=Bundle[mParcelledData.dataSize=3252]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=1
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-1m52s149ms
resizeMode=RESIZE_MODE_UNRESIZEABLE
Running activities (most recent first):
TaskRecord{90b88b8 #1352 I=com.example.binay.launcherdemo/.MainActivity U=0 StackId=1 sz=1}
Run #5: ActivityRecord{b1b0736 u0 com.example.binay.launcherdemo/.MainActivity t1352}
TaskRecord{e45d9f6 #1358 A=com.example.binay.launcherdemo U=0 StackId=1 sz=1}
Run #4: ActivityRecord{bcd0adc u0 com.example.binay.launcherdemo/.TestActivity t1358}
TaskRecord{d4c7864 #1357 A=com.apusapps.launcher U=0 StackId=1 sz=1}
Run #3: ActivityRecord{21fe202 u0 com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity t1357}
TaskRecord{fed9f82 #1348 A=com.apusapps.launcher U=0 StackId=1 sz=1}
Run #2: ActivityRecord{5f4526d u0 com.apusapps.launcher/com.apusapps.allapps.AllAppsActivity t1348}
TaskRecord{14ab4fc #1329 A=com.android.settings U=0 StackId=1 sz=2}
Run #1: ActivityRecord{34e44db u0 com.android.settings/.SubSettings t1329}
Run #0: ActivityRecord{8cc3587 u0 com.android.settings/.Settings t1329}
mResumedActivity: ActivityRecord{b1b0736 u0 com.example.binay.launcherdemo/.MainActivity t1352}
mLastPausedActivity: ActivityRecord{bcd0adc u0 com.example.binay.launcherdemo/.TestActivity t1358}
Stack #0:
mFullscreen=true
mBounds=null
Task id #1354
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{28ab6d0 #1354 I=com.example.binay.launcherdemo/.MainActivity U=0 StackId=0 sz=1}
userId=0 effectiveUid=u0a894 mCallingUid=1000 mUserSetupComplete=true mCallingPackage=android
intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x13200000 cmp=com.example.binay.launcherdemo/.MainActivity}
realActivity=com.example.binay.launcherdemo/.MainActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=1 mTaskToReturnTo=0
rootWasReset=true mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{63afa71 u0 com.example.binay.launcherdemo/.MainActivity t1354}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/1354_task_thumbnail.png
stackId=0
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=false firstActiveTime=1532172047076 lastActiveTime=1532172047076 (inactive for 16s)
* Hist #0: ActivityRecord{63afa71 u0 com.example.binay.launcherdemo/.MainActivity t1354}
packageName=com.example.binay.launcherdemo processName=com.example.binay.launcherdemo
launchedFromUid=1000 launchedFromPackage=android userId=0
app=ProcessRecord{3e17691 9030:com.example.binay.launcherdemo/u0a894}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x13200000 cmp=com.example.binay.launcherdemo/.MainActivity (has extras) }
frontOfTask=true task=TaskRecord{28ab6d0 #1354 I=com.example.binay.launcherdemo/.MainActivity U=0 StackId=0 sz=1}
taskAffinity=null
realActivity=com.example.binay.launcherdemo/.MainActivity
baseDir=/data/app/com.example.binay.launcherdemo-1/base.apk
dataDir=/data/user/0/com.example.binay.launcherdemo
stateNotNeeded=true componentSpecified=true mActivityType=1
compat={320dpi} labelRes=0x7f0b0027 icon=0x7f0a0000 theme=0x7f0c0005
config={1.0 ?mcc?mnc [en_IN,ta_IN] ldltr sw360dp w360dp h616dp 320dpi nrml long port finger -keyb/v/h -nav/h s.6}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff3f51b5
launchFailed=false launchCount=0 lastLaunchTime=-25s581ms
haveState=true icicle=Bundle[mParcelledData.dataSize=988]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true vi