上次开放活动

时间:2014-02-08 22:50:47

标签: android android-activity lifecycle android-lifecycle

启动时我的应用程序打开FirstActivity。

从FirstActivity我可以开始:SecondActivity,ThirdActivity。当我按下主页按钮然后返回我的应用程序时,它始终打开FirstActivity,而不是打开最后一个活动(在返回启动FirstActivity并onCreate()之后)。

如何在上次活动中恢复应用程序状态?

在代码中我没有启动FirstActivity。

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bvblogic.nimbusnote"
    android:versionCode="1"
    android:versionName="1.0">


    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17.0.0"
        android:maxSdkVersion="19" />
    <permission
        android:name="com.bvblogic.nimbusnote.activities.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.bvblogic.nimbusnote.activities.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <!--Permission for create shortcut on Home Screen-->
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"></uses-permission>


    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:largeHeap="true"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:theme="@style/Theme.NimbusNote.Light"
        android:hardwareAccelerated="true"
        android:label="@string/app_name"
        android:name=".application.App">
        <activity

            android:name=".activities.StartActivity"
            android:theme="@style/Theme.Sherlock.Light.NoActionBar"
            android:windowSoftInputMode="stateHidden"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.startactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>

        <activity
            android:name=".activities.FolderListActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.folderlistactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>
        <activity
            android:name=".activities.SettingsActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.settingsactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ExplorerActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.exploreractivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activities.PreviewNoteActivity"
            android:windowSoftInputMode="adjustPan"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.previewnoteactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.VIEW_NOTE"></action>

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".activities.EditNoteActivity"
            android:windowSoftInputMode="adjustResize"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.editnoteactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>
        <activity
            android:name=".activities.NoteInfoActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.noteinfoactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ChangePlaceMapActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.changeplacemapactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>
        <activity
            android:name=".activities.PlacesActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.placesactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>
        <activity
            android:name=".activities.ChangeTagsActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.changetagsactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>

        <activity
            android:name=".activities.TagsActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.tagsactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <activity
            android:name=".activities.PainterActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.painteractivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>

        <activity
            android:name=".activities.AccountSettingsActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.accountsettingsactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>

        <activity
            android:name=".activities.SyncSettingsActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.syncsettingsactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>
        <activity
            android:name=".activities.RecentNotesActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.bvblogic.nimbusnote.activities.recentnotesactivity" />
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>

        <service android:name=".services.FullSyncService" />
        <service android:name=".services.PrioritySyncService" />

    </application>


</manifest>

更新: 我从ExplorerActivity启动PreviewNoteActivity并按Home键并从快捷方式打开应用程序返回ExplorerActivity。但在按Back后,活动已关闭并显示PreviewNoteActivty。

Stack:

  • ExplorerActivity
  • PreviewNoteActivity

按Home并返回app:

  • ExplorerActivity
  • PreviewNoteActivity
  • ExplorerActivity

但重新启动应用后,我无法启动ExplorerActivity。 如果应用程序启动后我关闭应用程序与后退按钮问题已解决。但是在编译和第一次启动问题之后实际上是

更新2: 后退按钮监听器

  @Override
        public void onBackPressed() {
            if (mDrawerLayout.isDrawerVisible(mDrawerLeft)) {
                closeNavigationDrawer();

            } else if (!notesFragment.searchView.isIconified()) {
                notesFragment.searchView.setQuery("", true);
                notesFragment.searchView.setIconified(true);
                notesFragment.searchView.clearFocus();
            } else if (!mDrawerLayout.isDrawerVisible(mDrawerLeft)) {

                ActivityManager actM = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                List<ActivityManager.RunningTaskInfo> listm = actM.getRunningTasks(1);
                int iNumActivity = listm.get(0).numActivities;
                if (iNumActivity > 1) {
                    finish();
                } else {
 //close activity
                    closeAction();
                }
            }
        }

private void closeAction() {
        if (System.currentTimeMillis() - lastTimeBackPressed <= 2000) {
            App.isExplorerActivityStarted = false;
            finish();
        } else {
            lastTimeBackPressed = System.currentTimeMillis();
            Toast.makeText(getApplicationContext(), "Press again for exit", Toast.LENGTH_SHORT).show();
        }
    }

UPDATE3: 如果从apk文件(来自sdcard)安装应用程序,问题是紧急的。如果从IDE(Android Studio)安装问题则没有。

1 个答案:

答案 0 :(得分:2)

我找到了解决方案: 这是Android系统的错误。为了解决这个问题,我在onCreate()方法的下一个代码块中放入了root Activity(首次启动):

if (!isTaskRoot()) {
            Intent intent = getIntent();
            String action = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }