android - 判断 Activity 是从启动器启动还是导航回到后退按钮

标签 android android-activity android-intent

我试图在 onStart() 期间辨别我的 Activity 是从主屏幕启动还是从另一个 Activity“返回”。

getIntent().hasCategory("android.intent.category.LAUNCHER")

不起作用,因为 Activity 的 Intent 保持不变。

我想在主 Activity 开始时显示一个对话框,但我不希望每次用户在访问另一个 Activity 后返回主 Activity 时弹出它。

有什么办法可以做到这一点吗?

感谢您的帮助! -追逐

最佳答案

是的,只有当主要 Activity 第一次启动时才可以启动对话框,但我会在软终止时保存状态。因此,如果您在软杀时将状态保存在 onSaveInstanceState 中,您可以在 onCreate 中查找空包。如果 bundle 为空,则这是第一次启动。如果 bundle 不为空,那么您将从软杀返回。如果您在 onRetainNonConfigurationState 中保存状态,则代码如下所示:

    // RESTORE STATE HERE
    // Save state in onStop (prefs) and onRetainNonConfigurationInstance (ConfuseTextState)
    state= (ConfuseTextState)getLastNonConfigurationInstance();
    if (state != null) {  // at least second pass, get non view state from onRetainNonConfigurationInstance
        try {               
            this.isShowCharCount= state.isShowCharCount;
                this.timeExpire= state.timeExpire;
                this.timeoutType= state.timeoutType;
                this.isValidKey= state.isValidKey;
                this.password= state.password;
                this.isAutoLaunch= state.isAutoLaunch;
                //Log.d(TAG,"restoredStateFromLastConfiguration");
        }
        catch(Exception e){
            Log.d(Utilities.TAG,"FailedToRestoreState",e);
        }
    }
    else { // first pass, get saved state from preferences on first pass if they exist
        // Restore preferences (8) on hard kill when USER hit back and killed us
        SharedPreferences prefs = getPreferences(MODE_PRIVATE); // singleton
        if (prefs  != null){...
        }   // else state is from xml files and default instance values
        // SUPPORT EASY LAUNCH
        if (isAutoLaunch){ // launch on first show only
            this.showDialog(DIALOG_EASY_LAUNCH); //<== SHOW YOUR ALERT HERE!
        }
    } 

关于android - 判断 Activity 是从启动器启动还是导航回到后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5399424/

相关文章:

android - Android TV 应用中搜索球旁边的图标

java - 2d手游开发: OpenGL ES 2. 0还是cocos2d-x?

Android 常见的 Activity 和 FragmentActivity 父类(super class)

android - 即使我发送了一个 Intent ,OnActivityResult 也会在 Intent 通过时得到 null

android - 避免Android上的内存泄漏

android - FCM - 如何修复 android.app.RemoteServiceException : Bad notification posted from package Couldn't expand RemoteViews for: StatusBarNotification

java - 在最大没有。 Activity 和 fragment

android - 工具栏菜单项总是溢出

android - 如何在 android 中启动股票安装程序应用程序

android - 如何从uri获取文件名?