活动不会从暂停返回



我有一个包含三个活动的应用程序:启动画面(默认)、登录、主。应用程序以启动启动,几秒钟后更改为登录。如果登录过程正确,那么我们转到main。

我的问题是登录活动无法从暂停状态恢复。当我点击主页按钮时,onPause() 被正确调用,而 onDestroy() 没有被调用。然后,当尝试返回应用程序时,它会从启动开始,但从未达到登录状态,它只是返回到主页。logcat 不显示任何错误,调试器声明应用程序仍处于打开状态(它应该打开)。初始屏幕和主屏幕上的行为是预期的。

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
}

编辑:根据要求在启动画面中创建活动

 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();
}

}

发现并修复错误。该活动在清单中标记为单实例。我将其更改为SingleTop,现在它可以按预期工作。

有关原因的更多文档可以在这里找到:http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

SplashActivity中删除 finally 块,如下所示:

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();

我没有玩过太多线程,但从我的角度来看,您正在从 Splash 活动创建另一个线程,然后从该线程启动活动。然后,当您暂停时,您的新活动不知何故丢失或被垃圾回收(可能是因为它不在我不知道的主线程上),然后当您返回上一个活动时,不会再次调用onCreate,因此您有这个永久的启动画面(如果我正确理解您的问题)。

编辑我还要问你为什么要在那个线程内等待?

最新更新