简体   繁体   English

onResume()未在第二次调用时调用Activity

[英]onResume() not called second time an Activity is launched

During the normal course of development, I noticed that a particular activity appears to have stopped responding the second time it is called. 在正常的开发过程中,我注意到特定活动似乎在第二次调用时停止响应。

ie menu->(intent)->activity->(back button)->menu->(intent) menu->(intent)->activity->(back button)->menu->(intent)

There is nothing relevant in logcat. 在logcat中没有任何相关内容。

I don't even know where to start debugging this nor what code to show so here are the onClick and onResume fragments: 我甚至不知道从哪里开始调试这个也不知道要显示的代码是onClickonResume片段:

 if (!dictionary.getClassName().equals("")) {

            this.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    Intent i;

                    i = new Intent(mContext, NVGlobeNavigatorVC.class);
                    i.putExtra("PAGE_TITLE", title);
                    i.putExtra("TITLE", _dictionary._title);

                    mContext.startActivity(i);

            });
        } else {
            findViewById(R.id.greaterthan).setVisibility(View.GONE);
        }

and in the Activity being launched: 并在正在启动的活动中:

@Override
protected void onResume() {

    super.onResume();

    ...

Nothing unusual in manifest either: 清单中没有任何异常:

<activity
    android:name=".NVViews.NVGlobeNavigatorVC"
    android:theme="@style/WindowTitleBackground"
    android:label="GlobeNavigator"/>

For clarity, I put breakpoints on mContext.startActivity(i) and super.onResume() . 为清楚起见,我在mContext.startActivity(i)super.onResume()mContext.startActivity(i)断点。 I click the view with the onClickListener and both breakpoints are hit as expected. 我使用onClickListener单击视图,并按预期命中两个断点。 I then press the back button which returns me to the menu. 然后按下后退按钮,返回菜单。 onPause() is called as expected. 按预期调用onPause()

I touch the view to launch the activity again, and the breakpoint on startActivity is hit but not in onResume() in the target activity. 我触摸视图再次启动活动,并且startActivity上的断点被命中,但在目标活动中没有onResume() The screen goes black and the only way I can get going again is to restart the app. 屏幕变黑,我可以再次走的唯一方法是重启应用程序。 If I pause the debugger, it pauses in dalvik.system.NativeStart() which implies that the activity is never relaunched. 如果我暂停调试器,它会在dalvik.system.NativeStart()暂停,这意味着该活动永远不会重新启动。

I don't think it's relevant, but I'm using Intellij IDEA and have deleted all of the output directories, invalidated the caches and done a full rebuild. 我不认为它是相关的,但我正在使用Intellij IDEA并删除了所有输出目录,使缓存无效并完成了完全重建。

Target API is 8. I've tested on 2.3 and 4.0.4. 目标API是8.我已经在2.3和4.0.4上测试过。

Any ideas? 有任何想法吗? I'm stumped. 我很难过。

[EDIT] [编辑]

In onPause , I save some stuff to prefs. onPause ,我将一些东西保存到prefs。 The purpose of onResume() is to get them back again: onResume()的目的是让它们再次返回:

@Override
protected void onPause() {

   super.onPause();

   SCPrefs.setGlobeViewViewPoint(globeView.getViewPoint());
   SCPrefs.setGlobeViewZoom(globeView.getZoom());
   SCPrefs.setGlobeViewScale(globeView.getScale());

    }

I found the problem, and it's a bit esoteric. 我发现了这个问题,而且有点深奥。

I have a large static data structure which is loaded in a class static initialiser. 我有一个大的静态数据结构,它加载在类静态初始化器中。 There was a bug in that initialiser causing an infinite loop the second time it was called if the data structure was still loaded. 如果数据结构仍然加载,那么初始化器中有一个错误导致第二次调用它时出现无限循环。

Because that class is referenced in my Activity, the class loader is loading it before onCreate() or onResume() is called. 因为我的Activity中引用了该类,所以类加载器在调用onCreate()onResume()之前加载它。

The loop gave the appearance that the Activity loader had hung. 循环给出了Activity加载器挂起的外观。

This code: 这段代码:

i = new Intent(mContext, NVGlobeNavigatorVC.class);

creates a new intent. 创造一个新的意图。 The intent is of class NVGlobeNavigatorVC.class . 意图是NVGlobeNavigatorVC.class类。

If you call it once, you create a new activity, lets call it "iTheFirst". 如果您调用一次,则创建一个新活动,我们称之为“iTheFirst”。 If you back out of the activity, it executes "on pause". 如果退出活动,它将执行“暂停”。 When you run the above code again, you create another new activity of the same class, but a different acitivity. 当您再次运行上面的代码时,您将创建同一类的另一个新活动,但具有不同的活动。 Hence it won't resume your other activity, but make a new one that we could call "iTheSecond". 因此它不会恢复你的其他活动,而是创建一个我们称之为“iTheSecond”的新活动。 It looks just like iTheFirst but is unique. 它看起来就像iTheFirst,但却是独一无二的。

If you want to resume the above, after backing out of it, you need to keep a reference to it in your menu. 如果您想恢复上述内容,退出后,您需要在菜单中保留对它的引用。 Then in your onClick, look to see if that activity exists, and if not, make a new one and start it, and if it does exist, just resume it. 然后在你的onClick中,查看该活动是否存在,如果不存在,则创建一个新活动并启动它,如果它存在,则恢复它。

Here is a sample activity that remembers and restarts an activity: 以下是记住并重新启动活动的示例活动:

Intent savedCueCardActivity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, R.layout.landing);
}

public void goToNextScreen(View v) {
    if (savedCueCardActivity == null) {
        savedCueCardActivity = new Intent().setClass(this, CueCardActivity.class);
        startActivity(savedCueCardActivity);
        //       lastActivity.finish();
    } else {
        savedCueCardActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(savedCueCardActivity);
    }
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}

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

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