【问题标题】:how create homescreen shortcut to resume top activity如何创建主屏幕快捷方式以恢复顶部活动
【发布时间】:2014-04-29 21:22:15
【问题描述】:

我有一个小代码可以在第一次运行时添加到主屏幕的快捷方式:

    Intent shortcutIntent = new Intent(getApplicationContext(),
            SFlashActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "New App");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    addIntent
            .putExtra("duplicate", false);
    getApplicationContext().sendBroadcast(addIntent);

但是使用上面的代码,尽管我的应用程序正在运行,但我的应用程序总是启动启动画面。 那么我怎样才能让主屏幕快捷方式恢复到顶部活动。 我注意到,谷歌在安装时制作的应用程序快捷方式总是恢复顶部活动。

非常感谢!

【问题讨论】:

  • 您应该在应用程序第一次启动时将数据存储在“SharedPreferences”中。
  • 如果您的应用程序已经在运行,标准行为是恢复顶部活动。如果这没有发生,你就会发生一些奇怪的事情。我知道你已经接受了一个答案,但这个答案是多余的,不应该是必要的。将清单的内容添加到问题中。也许里面有什么讨厌的东西。
  • 另外请注意,如果应用程序最初是从安装程序或 IDE(Eclipse、Android Studio 等)启动的,Android 中存在一个错误会显示您所描述的行为。为确保您没有看到此错误:在设备上安装您的应用程序,不要通过单击安装程序屏幕上的“打开”来打开它,现在转到主屏幕并通过单击应用程序图标启动您的应用程序。看看你的问题现在是否消失了。见stackoverflow.com/questions/11296203/…

标签: android android-intent


【解决方案1】:

使用 isTaskRoot() 方法 在主要活动的 OnCreate() 中输入以下代码 sn-p 这是一个例子:


 @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.splashscreen);              
        if(!isTaskRoot()){
            finish();
            return; 
        }
 }

找到解决方案here:

【讨论】:

    【解决方案2】:

    当通过主屏幕上的图标启动时,Android 将始终使用 AndroidManifest.xml 中的 android.intent.action.MAIN 过滤器启动 Activity,除非应用程序已经在运行(在这种情况下,它显然会在堆栈)。

    要实现您所描述的,您可以简单地将最后一个可见活动存储在 SharedPreferences 中,并有一个 Dispatcher 活动根据偏好启动最后一个活动。

    因此,如果首选项中存在活动,则启动该活动或启动 SplashScreen。

    在您希望自动重新开始的每个活动中:

    @Override
    protected void onPause() {
        super.onPause();
    
        SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
        Editor editor = prefs.edit();
        editor.putString("lastActivity", getClass().getName());
        editor.commit();
    }
    

    还有一个类似于下面的Dispatcher活动:

    public class Dispatcher extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            Class<?> activityClass;
    
            try {
                SharedPreferences prefs = getSharedPreferences("X", MODE_PRIVATE);
                activityClass = Class.forName(
                    prefs.getString("lastActivity", SplashScreen.class.getName()));
            } catch(ClassNotFoundException ex) {
                activityClass = SplashScreen.class;
            }
    
    
            startActivity(new Intent(this, activityClass));
        }
    }
    

    备注

    • 您可以为 onPause 覆盖创建一个基类
    • Dispatcher 活动显然需要是 android.intent.action.MAIN 操作

    参考:-How to make an android app return to the last open activity when relaunched?

    【讨论】:

    • 感谢您的回答!我会尝试接受这个答案
    • 我仍然想知道为什么 google play 制作的快捷方式总是恢复顶部活动?
    • 他们挑衅地从那里做了一些逻辑来存储 Play 的当前状态,当你启动应用程序时,他们会检查最后一个状态。您可以验证这一点....从任何状态关闭播放。并转到播放设置并清除应用程序数据。现在打开 Play .. 你会看到你在 Play 的第一个屏幕上.. :D
    • 这不可能。如果你实现了这个,BACK 堆栈的处理将被完全破坏。另外,我不确定 OP 是否在谈论“播放”应用程序的快捷方式。我认为他正在谈论通过“播放”安装应用程序时自动创建的任何应用程序的任何快捷方式。
    • 你在这里做梦吗?如果他谈到任何快捷方式,他们为什么要提供他的代码来创建快捷方式?我错了吗?
    【解决方案3】:

    由于按下 Home 按钮,当应用程序处于后台时返回时,我也遇到了同样的问题,即恢复应用程序,

    要做的两个改变

    1.将以下属性添加到清单文件中的活动

    android:alwaysRetainTaskState="true" 
    

    这将在来自启动器图标时恢复活动。

    2.如果您单击以编程方式创建的主屏幕上的应用程序图标,则上部更改不会恢复应用程序。因为您已经为启动目的指定了“SFlashActivity.class”。要克服这个问题,您必须采取以下技巧:

    将此函数添加到您的 SFlashActivity.class

    public boolean CheckIfAppIsResumable(){
        try{
             ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
             List<RunningTaskInfo> runningTaskInfoList =  am.getRunningTasks(1);
             Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
             while(itr.hasNext()){
                 RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
                 CharSequence desc= runningTaskInfo.description;
                 int numOfActivities = runningTaskInfo.numActivities;
                 int numRunning=runningTaskInfo.numRunning;
                 String topActivity = runningTaskInfo.topActivity.getClassName();
                 Log.d("description", ""+desc);
                 Log.d("numActivities", ""+numOfActivities);
                 Log.d("numRunning", ""+numRunning);
                 Log.d("topActivity", ""+topActivity);
                 if(numRunning>1 && topActivity.equalsIgnoreCase("com.yourpackage.yoursplashclass"))
                     return true;
             }
             return false;
        }
        catch(Exception e){
            Log.d("Errror CheckIsAppIsResumable=", ""+e.getMessage());
            return false;
        }
    }
    

    在 Oncreate 中:

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if(CheckIfAppIsResumable()){
            finish();
            return;//will finish the new started splash activity
        }
    

    前提是您的快捷方式意图没有标志 FLAG_ACTIVITY_CLEAR_TOP。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      • 2011-09-14
      • 1970-01-01
      • 2014-08-12
      • 1970-01-01
      • 2018-03-29
      相关资源
      最近更新 更多