1

我正在开发一个应用程序。它是一个单页秒表应用程序。因此,当我按下主页按钮并再次启动它或从正在运行的应用程序运行时,它会创建新的应用程序实例。我想要以前的当前正在运行的实例。

我用谷歌搜索了这个,发现我需要设置启动模式。所以,我做了类似下面的事情。但没有任何工作。还是一样。

    <activity android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:launchMode="singleTask"  
        >  


@Override
protected Parcelable onSaveInstanceState()
{
    Log.d(TAG, "onSaveInstanceState");
    Bundle bundle = new Bundle();
    bundle.putParcelable(INSTANCE_STATUS, super.onSaveInstanceState());
    bundle.putDouble(STATUS_ANGLE, curAngle);
    return bundle;
}

@Override
protected void onRestoreInstanceState(Parcelable state)
{
    Log.d(TAG, "onRestoreInstanceState");
    if (state instanceof Bundle)
    {
        Bundle bundle = (Bundle) state;
        super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATUS));
        curAngle= bundle.getDouble(STATUS_ANGLE);
        curTime = (int) (60 / (2 * Math.PI) * curAngle* 60);
        return;
    }
    super.onRestoreInstanceState(state);
}  

还有一件事:它基于计时器。这是否意味着我的应用程序必须在后台运行?

4

1 回答 1

0

You should save the state of your activity using onSaveInstanceState, and load it back using onRestoreInstanceState. You can read about it at the developer docs.

于 2015-11-12T04:57:45.940 回答