我的应用程序正在创建新的意图,当我启动应用程序时,我想要我的旧应用程序(如果正在运行)

时间:2015-11-12 04:52:14

标签: android android-activity launchmode

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

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

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

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

1 个答案:

答案 0 :(得分:0)

您应该使用onSaveInstanceState保存活动状态,然后使用onRestoreInstanceState加载回来。您可以在developer docs了解相关信息。