Reputation: 2157
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:
After press Home and return to app:
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.
Upvotes: 0
Views: 753
Reputation: 2157
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;
}
}
Upvotes: 2