fix(home): 主界面按 Home 返回默认页改用交付 Intent 的焦点判断

- 与 Launcher3 一致:仅在已处于前台且非拖动翻页时回到默认页
- 移除 onStart/onStop 标记,避免从其他应用返回被误重置
This commit is contained in:
tongtong committed 2026-11-06 19:31:39 +08:00
1 parent e99f8a4e92
commit ed16b33f81
1 file changed
+14 -27
@@ -60,13 +60,7 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
private int mCurrentIndex = -1;
private int mFragmentSize = 0;
// 标记 MainActivity 是否已真正退到后台(经历过 onStop)。
// 用于区分两种场景:
// - 从其他应用按 home 返回(经历过 onStop,第一次)→ 保持当前 fragment 不重置
// - 已经在主界面时再次按 home(未经历 onStop,第二次)→ 回到默认 fragment
// 注意:不能用 onPause/onResume 判断,因为跳转其他应用时 onPause 会触发而 onResume 不一定,
// 且已在主界面按 home 时可能先 onPause 再 onNewIntent,会误判。
private boolean mIsStopped = false;
private boolean mPagerDragging;
private ScaleCircleNavigator mScaleCircleNavigator;
@@ -175,6 +169,7 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
@Override
public void onPageScrollStateChanged(int state) {
mPagerDragging = state == ViewPager.SCROLL_STATE_DRAGGING;
Logger.v(TAG, "onPageScrollStateChanged: state = " + state);
}
});
@@ -271,13 +266,18 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e(TAG, "onNewIntent: " + intent.getAction());
if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Logger.e(TAG, "onNewIntent: mIsStopped = " + mIsStopped);
if (!mIsStopped) {
// 仍在主界面(未经历 onStop),再次按 home 键 → 回到默认 fragment(类似 launcher)
mViewDataBinding.viewPager.setCurrentItem(mDefaultIndex, true);
}
// 否则:从其他应用按 home 返回(第一次,经历过 onStop)→ 保持当前所在的 fragment 不重置
// 与 Launcher3 一致,使用交付 Intent 时的窗口焦点和任务前置标志。
boolean alreadyOnHome = hasWindowFocus()
&& (intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) == 0;
if (Intent.ACTION_MAIN.equals(intent.getAction())
&& intent.hasCategory(Intent.CATEGORY_HOME)
&& alreadyOnHome && !mPagerDragging) {
ViewPager pager = mViewDataBinding.viewPager;
pager.post(() -> {
if (!mPagerDragging) {
pager.setCurrentItem(mDefaultIndex, true);
}
});
}
}
@@ -293,19 +293,6 @@ public class MainActivity extends BaseMvvmActivity<MainViewModel, ActivityMainBi
}
}
@Override
protected void onStart() {
super.onStart();
mIsStopped = false;
}
@Override
protected void onStop() {
super.onStop();
Logger.e(TAG, "onStop: ");
mIsStopped = true;
}
@Override
protected void onDestroy() {
super.onDestroy();