0

My application on boot opens FirstActivity.

From FirstActivity I can start: SecondActivity, ThirdActivity. When I press the Home button and then return to my app, it always opens FirstActivity, rather than opening the last activity (after returning start FirstActivity with onCreate()).

How I can restore application state on last activity?

In code I don't start FirstActivity.

My manifest:

<?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>

UPDATE: Than I'm start PreviewNoteActivity from ExplorerActivity and press Home button and open application from shortcut I return to ExplorerActivity. But after press Back, activity is closed and show PreviewNoteActivty.

Stack:

  • ExplorerActivity
  • PreviewNoteActivity

After press Home and return to app:

  • ExplorerActivity
  • PreviewNoteActivity
  • ExplorerActivity

but I don't start ExplorerActivity after app is restarted. If after app started and i close app with Back button problem is resolved. But after compile and first start problem is actually

UPDATE 2: Back button listener

  @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: The problem is urgent if install application from apk file (from sdcard). If install from IDE (Android Studio) problem there is not.

halfer
  • 19,824
  • 17
  • 99
  • 186
a.black13
  • 2,157
  • 4
  • 19
  • 20
  • 1
    What code do you use to move between Activities? – Graham Smith Feb 08 '14 at 22:58
  • 1
    it's normal mainfest i think the problem in code navigation or you implement back button method , could you post the code ? – mohammed momn Feb 08 '14 at 23:00
  • Wow, going a little wild with the intent filters there! Looks like the activity that will be launched from the launcher is ExplorerActivity. Why so many other intent filters? My guess is you don't need all of them, especially those that simply point to the activity that contains them. E.g. why have an intent filter in EditNoteActivity and NoteInfoActivity? When using your app, all you need to do is create an intent like Intent intent = new Intent(getActivity(), EditNoteActivity.class); to tell the OS that you're trying to go to that activity. – Justin Pollard Feb 08 '14 at 23:06
  • Between activities i'm use Intent = new Intent(getApplicationContext(), SecondActivity); startActivity(intent); – a.black13 Feb 08 '14 at 23:12
  • Many activities because large project with 15 activities. – a.black13 Feb 08 '14 at 23:13
  • My application have register many BroadcastReceivers (as like InternetConnection). Many intent filters because i have class Intents with static fields as like public static String EXPLORER_ACTIVITY = "com.bvblogic.nimbusnote.activities.exploreractivity" Intent start as: Intent = new Intent(Intents.EXPLORER_ACTIVITY); startActivity(intent); – a.black13 Feb 08 '14 at 23:16

1 Answers1

2

I found solution: This is Android System bug. For resolve this problem i put in root Activity (first started) in onCreate() method next code block:

if (!isTaskRoot()) {
            Intent intent = getIntent();
            String action = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
a.black13
  • 2,157
  • 4
  • 19
  • 20