Activity not closing properly - android

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray

That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)

- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

Related

Android skipping the splash screen when re-opening the app after having pressed back

So my scenario is as such.
Let's say there is a MainActivity, which only job is to start, call installSplashScreen().setKeepOnScreenCondition { true } to show the Splash screen using the new backward compatible APIs, and then after checking some state it does startActivity(SomeActivity); finish()
Now we're on the SomeActivity and if we press the home button, the app is gone on the background. Then if we click on the launched icon, the SomeActivity is launched correctly, and the MainActivity's onCreate is never called, therefore the splash screen does not show again, and the SomeActivity shows instantly.
But if instead of pressing the home button, we press the back button, and the app is backgrounded that way, then when we click on the launcher icon, the MainActivity's oncreate is called again, and the splash screen icon flashes for a tiny fraction too making it look jarring.
My question is, does this sound like it's some wrong configuration on my part, or am I stuck with this behavior as long as I am not on a single activity architecture?
You are confused. Pressing the BACK button does not "send the app to the background". The default behaviour of the BACK button (assuming that you don't override this and provide your own behaviour) is to finish the current Activity. Normally this will take the user to the previous Activity in the current task. In your case, there is no other Activity in the task stack, so the current task is empty. This may appear to the user as "the app is sent to the background", but in reality there is nothing in the background. Your app's task is empty and so it is gone.
When the user then taps the app icon on the HOME screen or taps on the app's task in the list of recent tasks, there is no existing task to bring to the foreground, so the app is started again by creating a new task and launching the root Activity (in your case MainActivity) into the newly created task.
If you want the BACK button to just put your app into the background (instead of calling finish() on SomeActivity, which is the default behaviour) then just override onBackPressed() in SomeActivity and do this instead:
moveTaskToBack(true);
It seems like there is no solution to what I am facing as long as the Activity I want to resume to is not the Launcher activity.
From the the docs:
"Root launcher activities are activities that declare an Intent filter with both ACTION_MAIN and CATEGORY_LAUNCHER. These activities are unique because they act as entry points into your app from the app launcher and are used to start a task.
System behavior on Android 12 and higher
The system moves the activity and its task to the background instead of finishing the activity. This behavior matches the default system behavior when navigating out of an app using the Home button or gesture."
Reading the docs about the new back behavior on Android 12 and onwards tells us that pressing back when you got nothing else on the stack will act as if you pressed the home button.
There's a big exception here, and that is that when you re-open the application, if the one you just popped was not the launcher activity, it will then still need to launch that launcher activity and can't resume from where you left off in a hot state, exactly the reason why I am seeing the splash screen again.
So I think my best bet is to either ignore this for now, or fix my app to be a single-activity app, or at least keep the launcher activity be the top-level one that you exit the app from by pressing back
To indicate a couple of examples, if one wants to experience what I mean, the reproduction steps are to:
Open the app
Press the back button which will send you out of the app to the home screen
Click on the app icon again
As of today, apps like Google Photos, and Google Podcasts don't show the splash again. In contrast, apps like Google Maps, Twitter, Spotify do show the splash again for a brief second.
to call the launcher activity every time you have to clear the stack that means you have to use a flag in your manifest to tell your app not to keep activity in background try android:launchMode="singleTask" inside your activity tag in manifest the activity that you want to be killed everytime you go to background, and as far as how much time splash should be showing you can use timer for that after the timer is finished then your someActivity will be called.

Why does "Home" button call `onDestroy()` in my Android app?

On the Android dev page, it says pressing the "Home" or "Overview" button does not invoke onDestroy,
but in my app, it keeps calling onDestroy. Are there any clues?
(detail situation below)
I've built a simple app that switches from the main activity to a second activity,
but if I press the "Home" or "Overview" button on the second activity, the onDestroy gets called.
So when I go back to my app again, it shows the main activity, not the second activity.
Is this normal?
Should I save the state if I want to go back to the last activity (not the main activity) after pressing the Home or the Overview button and coming back to my app?
Android dev page that I read:
If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().
and
Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.
So it is supposed to invoke only onPause and onStop, not onDestroy, isn't it?
Finally I found the culprit!
the problem was that I set android:noHistory="true" on the second activity, in the AndroidManifest.xml file.
Making this option true let the activity not leave the history,
so if another activity comes to the foreground and the user pushes the back button, the previous activity(noHistory=true) does not show up.
Similarly, if the user pushes the Home or the Overview button, then the user tries to come back to our app, the last activity(noHistory=true) does not show up either.
You have to put the Code in your Question or we can't help you.
Maybe you are calling finish() in MainActivity after you call startActivity(MainActivity.this, SecondActivity.class) ?
Use the edit-function and show us your code then we can help you more.

Why activity come back to front via onCreate rather than onRestart?

What I want to do is to start an Activity upon device restarts by sending an intent in a BroadCastReceiver listening BOOT_COMPLETED event. The Activity has a conditional moveTaskToBack in its onCreate event handler. The Activity is launchable and the only Activity of App.
When I reboot the device, App is running and I can tell Activity is hidden from screen. From logcat a 'onCreate' message is print to indicate onCreate event happens. Then I click App icon in screen, expect Activity shows up from back for onCreate shall be omitted and moveTaskToBack shall NOT run. But onCreate event handler is still executed and Activity hides again.
In another way I remove starting Activity from BroadCastReceiver, just open App by click icon in screen. At first tap App starts running and Activity hides, and when I tap icon again, the Activity shows up. From logcat the first event is onRestart, which is expected.
I am not sure what is the different between two ways of bring back Activity? Why onCreate happens twice applying BroadcastReceiver in first case?
Cheers!
I'm now aware that the activity started by broadcast receiver is in a stack, while the activity, which is as same class, started by launcher is in another stack. Thus why onCreate event happened again when I tapped App icon desktop, for App create a new stack and brand-new activity.
Thanks xvlcw for great help...

Android app navigates to launch activity when coming from background

I have 3 activities ( say A, B, C were A-is the Launch activity). When I press home button when at activity C, app goes into background. After that, I took my app through all apps list menu. At that time, my launch activity is showing (activity A). When I press back button, It goes to previous activity (C). I want to retain in the same activity (C) while coming back from background. When I run application through Eclipse, It works fine. But when I send Apk file through mail and run it in device, It fails (previous problem occurs ).
I tried with
android:launchMode="standard"
AND
android:alwaysRetainTaskState="true"
in my launch activity (Login activity or A). Any body please help me, Thanks in advance.
Follow following steps to insure that you are following the right practice:
1. Make sure you are using finish(); on the Activity A or B if you want to finish it and dont if you want the back button functionality.
2. Try Implementing onpause() and onresume() even if you are not going to perform any functionality in them. Use super() for them there.
3. Also, in android when you start an activity by clicking on the icon instead of resuming it from already running activities, it exhibits a different behaviour.

Bug in ActivityManger in android 2.1 when using long press on Home Key

In one of my services I fire an event that puts a notification in the status bar which includes a pending intent to start an activity when the user clicks the notification.
The activity I'm starting is actually a "popup" that has the theme of a dialog popup (android:theme="#android:style/Theme.Dialog") defined in the manifest. The code for the pending intent is below:
Intent intent = new Intent(this, PopupWindow.class);
PendingIntent launchIntent = PendingIntent.getActivity(context, 0 , intent, 0);
notificationManager.notify(notificationRef, notification);
Everything works fine in android 2.2, but when testing in android 2.1, the newly started popup window doesn't take focus on the screen.
I know the activity is starting, because if I hold the home button down to bring up recently started apps, the "popup" will magically appear and takes focus.
Is there something I'm missing here? Why does my code work in android 2.2 and not 2.1?
After a day of debugging, I found that the new activity launched from the pending intent (Activity B in the stack) would get lost somewhere in the ActivityManager behind either the already opened activity (Activity A) or possibly the window of recently opened apps (long press on home key).
This happened ONLY if navigated away from Activity A with the use of a long-press on the home key ONLY in versions of android < 2.1. Every other instance of navigating away from Activity A (home key short-press, back-key press) would allow the pending intent on Activity B to open and take focus above everything on the screen. In android 2.2 and above, the code works absolutely fine with no issues. Very weird. To make things even weirder, if I put a Toast message to be displayed within the onRestart method of Activity A, the issue completely disappeared. There is nothing weird going on inside the onPause method of Activity A either....I still don't know.
I tried almost all flags on the pending intent for Activity B, but none of them would allow the popup to get to the top of the activity stack....I think Nanne and willytate put me on the right track...
I abandoned the method of setting the pending intent inside of a service, I think it was breaking the affinity between Activity A and Activity B. When I set the pending intent for Activity B inside of Activity A, as apposed to inside the service running in the background, Activity B (in the form of a popup via android:theme="#android:style/Theme.Dialog" in the manifest) would always appear at the top of the stack.
Once again, this "loss of focus" on an activity has only ever happened to me in this process:
Android 2.1
Launch Activity A
Use service to set status bar notification with a pending intent to start Activity B
Navigate away from Activity with long press on home key to any other application.
Service fires notification in status bar with intent to start Activity B.
Click notification to open Activity B.
Activity B, created as a dialog, is no where to be found and Activity A comes to the stop of the stack, but remains paused. No response to user touch at all on Activity A and the timer being displayed isn't moving. The Activity is stuck in onPause maybe?
The only way to get Activity B to appear is to, once again, long press the home key. When this is done, I see the following on the screen, in descending order with the first as the one at the top of the stack ready for user input :
List of recently used apps.
Activity B.
Acitivyt A.
Pressing the back button to dismiss the list of recently used apps will allow Activity B to take user input, and from there everything runs normally.
If this was not a complete waste of time, I did get a much better understanding on how Android handles the application stack.
Can anyone else (if they really wanted to) re-create this issue?

Categories

Resources