简体   繁体   English

android 返回活动时它被破坏并且不恢复

[英]android when returning to activity it is destroyed and not resumed

I am building a android application with two Activities utilizing the Action Bar (https://github.com/johannilsson/android-actionbar as i am targeting Android 2.2).我正在构建一个 android 应用程序,其中包含两个使用操作栏的活动(https://github.com/johannilsson/android-actionbar,因为我的目标是 Android 2.2)。

It has a number of activities.它有许多活动。 There is a "Home" Activity, called Feed, and another activity called "Settings".有一个名为“Feed”的“主页”活动和另一个名为“设置”的活动。

The problem i am having is that using the createIntent function that is given in the sample for the action bar i am using, the Activity still get destroyed rather than resumed when the user taps the home button to return to the Feed activity.我遇到的问题是,使用示例中为我正在使用的操作栏提供的 createIntent function,当用户点击主页按钮返回到 Feed 活动时,活动仍然被破坏而不是恢复。

With a bit of debugging i found that it is getting destroyed, not when the Activity is first paused and stopped, but when the request for it to resume happens.通过一些调试,我发现它正在被破坏,不是在第一次暂停和停止 Activity 时,而是在请求它恢复时发生。

public static Intent createIntent(Context context) 
{        
    Intent i = new Intent(context, Feed.class);        
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        
    return i;    
}

This is a major annoyance, and considerably slows down the application as it has to reload its data for the feed (which is cached, but its not instant to get the data reloaded).这是一个主要的烦恼,并且大大减慢了应用程序的速度,因为它必须重新加载它的数据以供提要(它被缓存,但它不是立即重新加载数据)。

So how can i avoid this behavior?那么我该如何避免这种行为呢? and why is this happening, as i believe the extra flag there should stop this behavior.以及为什么会发生这种情况,因为我相信那里的额外标志应该阻止这种行为。

You want to use:你想使用:

FLAG_ACTIVITY_REORDER_TO_FRONT

Quote from Android docs :引用 Android 文档

If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.如果在传递给 Context.startActivity() 的 Intent 中设置,则此标志将导致已启动的活动被带到其任务历史堆栈的前面(如果它已经在运行)。

For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.例如,考虑一个由四个活动组成的任务:A、B、C、D。如果 D 调用 startActivity() 的 Intent 解析为活动 B 的组件,则 B 将被带到历史堆栈的前面,结果顺序为:A、C、D、B。如果还指定了 FLAG_ACTIVITY_CLEAR_TOP,则此标志将被忽略。

public static Intent createIntent(Context context) 
{        
    Intent i = new Intent(context, Feed.class);        
    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);        
    return i;    
}

Depending on which class is launching what, you might be having an issue where a new instance of your activity is being launched and the old one is being destroyed.根据哪个 class 正在启动什么,您可能会遇到一个问题,即您的活动的新实例正在启动而旧实例正在被销毁。 This is what the documentation says about FLAG_ACTIVITY_CLEAR_TOP :这就是文档所说的FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.如果设置,并且正在启动的活动已经在当前任务中运行,那么不会启动该活动的新实例,而是关闭它上面的所有其他活动,并且此 Intent 将被传递到(现在顶部)作为新意图的旧活动。

Notice that it says "if the activity being launched is already running in the current task ..." So I'm not sure, but this would make sense if your debugging has shown you that the activity is also being destroyed.请注意,它说“如果正在启动的活动已经在当前任务中运行......”所以我不确定,但如果您的调试显示活动也被破坏,这将是有意义的。

When debugging, are you looking at the "id" numbers for your class objects?调试时,您是否正在查看 class 对象的“id”数字? Set a breakpoint in both the onCreate() and onDestroy() methods of your class and look at the id of the class inside each one.在 class 的onCreate()onDestroy()方法中设置一个断点,并查看每个方法中 class 的 id。 If they are different, then you know you have two different instances of your activity.如果它们不同,那么您知道您有两个不同的活动实例。

As a side note/question, how are you "targeting" 2.2 if the ActionBar wasn't made available until 3.0?作为旁注/问题,如果ActionBar直到 3.0 才可用,您如何“定位” 2.2?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM