MLProgrammer-CiM
MLProgrammer-CiM

Reputation: 17257

Activity doesn't return from Pause

I have an app with three activities: splashscreen (default), login, main. The application starts with splash, after some seconds changes to login. If the login process is correct then we go to main.

My problem is the login activity cannot be recovered from a paused state. When I hit the Home button the onPause() is called correctly and onDestroy() is not called. Then when trying to return to the application, it starts on splash but never reaches login, it just goes back to Home. The logcat doesn't show any error and the debugger states the application is still open (like it should). The behavior on the splash and main screens is the expected.

public class LoginActivity extends Activity {
/* UI ELEMENTS */
private OnClickListener mOnClickListener;

private EditText mPasswordField;

private EditText mUserField;

private ProgressDialog mProgressDialog;

/* LOGIC ELEMENTS */
/** handler to update interface */
private static Handler sInterfaceUpdateHandler;


public static class UpdateHandler extends Handler {

    private final WeakReference<LoginActivity> mLogin;

    UpdateHandler(final LoginActivity loginActivity) {
        super();
        mLogin = new WeakReference<LoginActivity>(loginActivity);
    }

    /**
     * handle events from other threads in UI thread.
     * 
     * @param message message data. Property what determines action.
     */
    @Override
    public void handleMessage(final Message message) {
       // STUFF HERE
    }


@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.initializeInterface(); // fields filled here, listener added to buttons
}

EDIT: Activity creation in SplashScreen as per request

 public class SplashScreen extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.splashscreen);
    final Thread splashThread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while (waited < 2000) {
                    sleep(100);
                    waited += 100;
                }
            } catch (final InterruptedException catchException) {
                LoggerFactory.consoleLogger().printStackTrace(catchException);
            }
            SplashScreen.this.finish();
            final Intent loginIntent = new Intent(SplashScreen.this, LoginActivity.class);
            SplashScreen.this.startActivity(loginIntent);
        }
    };
    splashThread.start();
}

}

Upvotes: 4

Views: 476

Answers (3)

MLProgrammer-CiM
MLProgrammer-CiM

Reputation: 17257

Error found and fixed. The activity was marked as SingleInstance in the manifest. I changed it to SingleTop and now it works as expected.

More documentation on the cause can be found here: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Upvotes: 3

Raigex
Raigex

Reputation: 1215

I haven't played with threading much but from what it looks like to me, you are creating another thread from the Splash Activity and then starting an activity from that thread. Then when you go to pause, your new activity is somehow getting lost or garbage collected (maybe because it is not on the main thread i dont know) then when you return to the previous activity, onCreate is not called again, thus you have this perpetual SplashScreen (if I'm understanding your problem right).

Edit also I have to ask why you are waiting inside that thread?

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

Remove the finally block from your SplashActivity and as below :

final Thread splashThread = new Thread() {
    @Override
    public void run() {
        try {
            int waited = 0;
            while (waited < 2000) {
                sleep(100);
                waited += 100;
            }
        } catch (final InterruptedException catchException) {
            LoggerFactory.consoleLogger().printStackTrace(catchException);
        } 
            SplashScreen.this.finish();
            final Intent loginIntent = new Intent(SplashScreen.this, LoginActivity.class);
            SplashScreen.this.startActivity(loginIntent);
        }
};
splashThread.start();

Upvotes: 0

Related Questions