Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onResume() not called second time an Activity is launched

Tags:

android

During the normal course of development, I noticed that a particular activity appears to have stopped responding the second time it is called.

i.e. menu->(intent)->activity->(back button)->menu->(intent)

There is nothing relevant in logcat.

I don't even know where to start debugging this nor what code to show so here are the onClick and onResume fragments:

 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(). I click the view with the onClickListener and both breakpoints are hit as expected. I then press the back button which returns me to the menu. onPause() is called as expected.

I touch the view to launch the activity again, and the breakpoint on startActivity is hit but not in onResume() in the target activity. 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.

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.

Target API is 8. I've tested on 2.3 and 4.0.4.

Any ideas? I'm stumped.

[EDIT]

In onPause, I save some stuff to prefs. The purpose of onResume() is to get them back again:

@Override
protected void onPause() {

   super.onPause();

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

    }
like image 447
Simon Avatar asked Mar 14 '13 21:03

Simon


2 Answers

This code:

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

creates a new intent. The intent is of class NVGlobeNavigatorVC.class.

If you call it once, you create a new activity, lets call it "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". It looks just like iTheFirst but is unique.

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.

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);
}
like image 159
HalR Avatar answered Oct 07 '22 02:10

HalR


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.

The loop gave the appearance that the Activity loader had hung.

like image 37
Simon Avatar answered Oct 07 '22 01:10

Simon