活动不会从暂停返回

时间:2013-01-23 10:27:46

标签: android android-activity

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

我的问题是登录活动无法从暂停状态恢复。当我点击Home按钮时,正确调用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
}

编辑:根据请求在SplashScreen中创建活动

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

}

3 个答案:

答案 0 :(得分:3)

找到并修复了错误。活动在清单中标记为SingleInstance。我将其更改为SingleTop,现在它按预期工作。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我没有玩过很多线程,但从我看来,你是从Splash Activity创建另一个线程,然后从该线程开始一个活动。然后,当你去暂停时,你的新活动会以某种方式丢失或收集垃圾(可能是因为它不在我不知道的主线程上)然后当你返回上一个活动时,onCreate不再被调用,因此你有这个永久SplashScreen(如果我正确理解你的问题)。

编辑我也要问你为什么要在那个帖子里等?

相关问题