【问题标题】:My app is creating new intent, I want my old intance of app (if running) when I start app我的应用程序正在创建新意图,当我启动应用程序时,我想要我的旧应用程序实例(如果正在运行)
【发布时间】:2015-11-12 04:52:14
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: android android-activity launchmode


    【解决方案1】:

    您应该使用onSaveInstanceState 保存您的活动状态,并使用onRestoreInstanceState 将其加载回来。你可以在developer docs阅读它。

    【讨论】:

    • 我已经有了这两个功能。但仍然无法正常工作。我也想继续运行我的应用程序。这样我就可以得到当前的运行状态。
    • 另一件事是基于计时器。这是否意味着我的应用必须在后台运行?
    • 我有点迷糊,方法onSaveInstanceState的签名是这样的:protected void onSaveInstanceState (Bundle outState) NOT protected Parcelable onSaveInstanceState()
    • 哦,我刚刚浏览了文档才知道,这个方法在 View 类中。您必须为 Activity 保存和恢复实例。
    猜你喜欢
    • 2017-02-11
    • 2020-11-28
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多