Activity in background gets killed when Home button is pressed - android

I encountered strange problem, lets say I have two activities A and B, app starts with Activity A, I proceed to activity B press Android Home Button, return to app which brings me back to Activity B. Then I press Back button (either hardware on in toolbar) and this closes app, but it should return me to Activity A. Activity B has no override of onBackPressed and has Activity A stated as PARENT_ACTIVITY in manifest. I'm starting it with Intent with no flags. Any idea why this happens ? Thanks

The behaviour of back buttons depends on system version. There is support for providing back navigation in older Android versions, described here:
https://developer.android.com/training/implementing-navigation/ancestral.html
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
The best and most convenient way to debug back stack issues is to enable 'Don't keep activities' option in developer options.
That's my best guess. Good luck!

In order to run a new activity without destroying the old one, you have to add the flag FLAG_ACTIVITY_NEW_TASK to the intent that will run the activity:
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
as when setting this flag:
this activity will become the start of a new task on this history
stack. A task (from the activity that started it to the next task
activity) defines an atomic group of activities that the user can move
to. Tasks can be moved to the foreground and background; all of the
activities inside of a particular task always remain in the same order.
so the activity which started it will remain in the stack, and hence you can call it again, and hence it also can be called automatically again when pressing BACK_BUTTON even if you pressed the HOME_BUTTON earlier.
and you have to combine #gduh answer with mine, as for sure you must make sure that you are not calling finish(); in ActivityA while calling the ActivityB.

thanks for help, problem was caused by this flag for activity in manifest android:launchMode=singleinstance (it's not originally my project so I missed that, I just hope I didn't screw something else up by removing it)

In your activity A when you call your activity B, maybe you have the following command :
finish();
If yes, you should remove this line. Then when you press back key from your activity B you should return A.
If not, maybe try to share your code.

Related

android back to last opened activity when click on app icon

I have looking for the some suggestions for back to last opened activity when launch from app icon. But still has some side effects to handle.
LauncherActivity is the entry activity which listen for
<activity
android:name="com.app.ui.LauncherActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTask"
android:screenOrientation="nosensor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then there is another second activity and third activity.
My scenario is as below:
1. LauncherActivity -> SecondActivity
2. SecondActivity -> ThirdActivity (When launching ThirdAcitivity, SecondActivity will finish itself)
So when at ThirdActivity, user click home key.
If the app is opened from recentTask, then the last opened ThirdActivity will be shown. This is the behavior what i want.
But when the app is opened from the app icon shortcut, then it take to the LauncherActivity. But i want it to be the same behavior as recentTask. If the last opened ThirdActivity is not being destroyed, then it should show this one instead of LauncherActivity.
However if the app force stop and then restart or ThirdActivity is being destroyed, then no need to go back to ThirdActivity. Back to LauncherActivity is ok.
I have been looking for solution. The suggestion of saving last activity to sharedPreference is not good for my case.
I would like to know how to achieve like launch from recent task. Is any one can guide me. Thanks a lot.
In your Launcher activity onCreate() method, write this:
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
isTaskRoot() is true when there are no activities in activity stack. In your case, if another activity (Third activity in your case) is there in Activity stack, above code will finish the launching activity and will bring Third Activity to screen.
You can handle this by having three fragments in one activity. Let's that activity be your LaunchActivity, then it has LAUNCHER intent-filter and always opens when you click app icon. If you had three fragments in that activity then the last fragment is opening just like when you open your app from recent tasks.
You could also remove launchMode="singleTask". Most apps don't need it.

Activity back stack not restored after killing process and resuming

I have some Activites that are started from my main Activity. When I go to one of these Activities, press home, kill the process, return to the app (where it resumes on Activity I left from), and then press back, it just closes my app instead of going back to my main Activity. I expect the back stack to be preserved. Does anyone know why this is happening?
I'm doing 3 things that might be related:
(1) My main Activity has launch mode singleTask
I tried removing this, but it didn't solve the problem. For example:
<activity
android:name=".activities.ActivityMain"
android:launchMode="singleTop"
android:screenOrientation="portrait">
</activity>
(2) I'm overriding onBackPressed() in the secondary Activity, but calling super.onBackPressed() when I do indeed want to go back. I also tried not doing this, but it also didn't help. For example:
#Override
public void onBackPressed()
{
Logger.d("ENTER");
ViewUtils.closeKeyboard(this);
//kv If it's a valid location, but hasn't changed, then just finish up
if (((FragmentLocation) getFragment()).isValidSpecifiedLocation()
&& !((FragmentLocation) getFragment()).sendUserSpecifiedLocation())
{
finish();
overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
}
}
(3) I'm setting the parent Activity in the manifest to my main Activity (I've also tried removing this, but it doesn't help):
<activity
android:name=".activities.ActivityLocation"
android:screenOrientation="portrait"
android:parentActivityName=".activities.ActivityMain">
</activity>
Any ideas?
UPDATE:
I tried recreating a very simple example with these conditions, but couldn't reproduce. I'm not sure what's causing this issue at this point.

Stopping multiple activities from showing up

I have it so when you click the notification icon on my app it opens up an activity. The thing is that if you click it over and over it opens the same activity over and over. So when you click the back button you have to go through all of those before going back to the beginning. How could I make it so that if that activity is already there it doesn't reopen it?
Check out the launchMode section of the manifest documentation.
You're looking to add:
android:launchMode="singleTop"
to the declaration for your Activity in the manifest. This means that if an instance of the activity is already at the top of the target task, the system will re-use that. Any intent gets delivered to the onNewIntent method of that activity.
I'd recommend having a good read of all the options on that page and deciding which one is right for your usage.
Try putting this in your manifest:
android:launchMode="singleTop"

Android Home button not resuming last Activity

I am trying to solve an issue sort of similar to what was done here (link text)
Except in my case where Activity A, started Activity B, started Activity C, when the user resumes the app after pressing the home button - I want C, the last Activity, to be displayed. Instead what is happening is I am back to A.
Also on a side note I have discovered that even when I exit the application, if I hold down the Home button, my app is still listed as an active app even though in the LogCat I can see that it exited OK.
Also, in case it matters, I did recently add android:launchMode ="singleTask" in order to make some notification logic work correctly. I was seeing the Home button behavior before that though.
I really appreciate any ideas you might have.
[edit - adding some code snippets]
<activity
android:name =".connection.ActivityA"
android:label ="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="#android:style/Theme.NoTitleBar"
android:launchMode ="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name ="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name =".screens.AvtivityB"
android:label ="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="#android:style/Theme.NoTitleBar"
android:launchMode ="singleTask"
android:screenOrientation="portrait" >
</activity>
<activity android:name =".screens.ActivityC"
android:label ="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:theme ="#android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" >
</activity>
Then here is how I kick off each Activity:
In Activity A:
private void startActivityB()
{
Intent activityBIntent = new Intent(this, ActivityB.class);
this.startActivityForResult(activityBIntent , ACTIVITYB_TAG);
}
Activity B actually has several Activities that it chooses from (it has a list of Intents) to display based on input it receives but launches them all the same way as Activty A launched B. Once the first Activity is displayed the user swipe the screen to navigate to the left or right screen. When the user swipes left or right, the current activity puts info in its return Intent that Activity B uses to display the next Activity. Also since logic outside of the user's control could prompt a change in the screen displayed, each Activty calls finish on itself in onStop to avoid back-button issues.
When you long press home it is showing a list of recently active applications, not applications that are currently running. So you should still see your app in the list, even if it is closed.
As the docs note:
The other modes — singleTask and
singleInstance — are not appropriate
for most applications, since they
result in an interaction model that is
likely to be unfamiliar to users and
is very different from most other
applications.
I'd imagine that your using singleTask is affecting the history stack, and thus preventing Activity C from being remembered. If you remove that from Activity C, does it resolve the problem?
Otherwise, I'd look at what you are doing in onPause/onDestroy etc for Activity C. Are you doing something that would cause it to finish or close?
Do you get this behavior only when running from Eclipse?
Check your "Run Configurations". There is a "Launch Action" that can be set to "Do Nothing".
Matt
I'm not sure this is what it happening to you, but the answer below solved my problem and I wanted the same behavior as you - that is, getting to the same activity when re-opening after home has been pushed (though in my case, I did actually want the first activity, just not a new one):
Save activity state in android when home button pressed
Now I realize you want to keep the activity stack intact and not go to the first activity like me. But if it is the same problem, then it is not from the fact that the activity stack is torn down, but because a new one (with the opening activity) is added when re-opening after home has been pressed on first install. I hope this is fairly legible, even though I (clearly) found it difficult to explain.
Edit: This is better described here and here.

How to clear stack back to root activity when user leaves application?

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force the user to go through each time they return to the application. I've set android:clearTaskOnLaunch="true" on LogonAct.
When I first start the app I go through this sequence of screens,
Home -> LogonAct -> MainAct -> Home
I then follow this sequence,
LogonAct -> Back -> MainAct
Why is it bringing me back to MainAct? Shouldn't that activity haven been closed since LogonAct has android:clearTaskOnLaunch="true". I expected to be brought back to Home when I hit the Back button from LogonAct.
Relevant snippets from AndroidManifest.xml,
<activity android:name=".LogonAct"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainAct">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResults" />
</activity>
I'm using 1.5.
Any help appreciated.
You can do following:
1. set clearTaskOnLaunch = "true" in AndroidManifest, in declaration of main activity
2. in activity that must close:
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
so if user presses back - it comes back to homescreen
if user launches aplication again - task stack clears and he comes to root main activity
The docs for android:clearTaskOnLaunch mention that this attribute applies "whenever [the Activity] is re-launched from the home screen".
However, in your case you're pressing the Home button to return to the Home screen, rather than pressing the Back button. This means your application isn't actually relaunched because the MainAct was not "finished". That only happens when you press Back (or if Android kills the task to save resources etc.).
As you only have two activities in your application, you could set the android:noHistory attribute on MainAct, thus ensuring that users can never return to it and must pass through the LogonAct.
As an aside, it seems a bit annoying to force users to re-login every time they navigate away from the app (for example when they receive a phone call).
You could retain a session token with timeout in your app's persistent storage, or hold a network connection open using a service if that's how your app works — but of course that's up to you and your requirements. :)

Categories

Resources