I've recently started developing for the Android platform and am currently praticing with an application that sync with Google Tasks. Right now, I'm not facing too much problem, technically speaking. But I meet a conceptual problem that I can't find a proper way to solve.
Let's say the user uses my application with a given Google account. He launch some activities, do some work...and then click the Home button. He then goes to the OS Settings and delete its Google accounts. Then he returns to my app, which then display the activity he was using when he did quit the app.
Since there is no more Google Accounts, my application should present the "Add Account" activity to allow him to choose or create a Google Account. And of course, if he tap on the back button at this point, he should be sent to the launcher and not to the previous activity from the back stack.
How would you deal with that kind of need?
I first thought that it was possible to be notified when my app was back to the foreground but it seems that Android always deals with activities, which would mean that I have to implement an "account checker" on all my activities ! Moreover, even if I implement this, how would I prevent the user from returning to the back stack and be instead redirected to the launcher when he taps the back button?
If some of you can give me some advices, some Best Practices, to deal with this, you would make my day.
PS : I just checked the Android 4 included GMail app and when I delete all my Google Accounts and then launch the app, I'm presented with the system "Add Google Account" activity and taping the back button send me to the launcher. That's exactly the behavior I'd like to implement. I suppose this app is not open-source, right?
onResume will be called when the activity comes to the foreground and onPause will be called when the activity is pushed to the back stack of activities.
You could always check the account status onResume. I would then recommend that you extend the activity class and make a BaseClass that has your google checker in it so every activity you want to have the check has it.
As far as the back button you can register a listener for the button press you can even ignore the back button if you are so inclined(not recommended but allowed by the sdk).
Related
I have my own pin code activity in my app which popup every time user returns to the app (on first start or when the app was in background). I found it curious that some bank apps can somehow detect when user just jumped to another app via "Recent app" and then return to the bank app (in this case PIN activity will not show up in these apps). How did they do it that when returned from Recent Apps, pin code activity will not be triggered?
I tried androidX´s LifecycleObserve and ComponentCallbacks2.
Currently I´m using registerActivityLifecycleCallbacks to generally detect when user leaves the app, but I have not idea, how to programmatically differentiate these two cases? Any ideas?
I have my own pin code activity in my app which popup every time user returns to the app (on first start or when the app was in background). I found it curious that some bank apps can somehow detect when user just jumped to another app via "Recent app" and then return to the bank app (in this case PIN activity will not show up in these apps).
What makes you so sure they're detecting that you came back from "recent apps"? If you go to a bank app, press back to close the app, then re-launch it from the list of apps, you may not be prompted for a pin or password.
If you leave the app for long enough and come back to it from "recent apps", you may be prompted for a pin or password.
More than likely these apps are employing some kind of time-based heuristic, although this will of course vary by app.
How did they do it that when returned from Recent Apps, pin code activity will not be triggered?
I don't know how "they did it", but as for the the question of "how can I know my activity was launched from recents"? Try looking for FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY in your intent.
Hope that helps!
I'd like my app to be able to launch any other app - but instead of launching the other-app's main activity (as described here), I'd like to be able to launch the other-app's most recent activity.
In other words, I want to programmatically have the same user experience that the user gets when clicking on the "recent apps" system/hardware button, and selecting any app from the list.
Not sure if that's even possible with the current Android API, or perhaps I didn't use it the right way.
Thanks!
It depends of the launchModeof the other app´s activity !
I'm working on large project, so there is some logic for saving application state, and then opening correct activity(fragment) when it comes from background.
But anyway, I've found that if user navigates through my app and then minimize it - android opens it from background in different ways in following cases:
User taps on app icon (behavior: user see home activity, doesn't matter where he was, when application was minimized)
User select app from android "recent apps" (behavior: user see exactly what he saw, when the application was minimized)
So, what is the difference between launching application from background by this two ways? I always thought, that it is the same mechanism, but, obviously, I was wrong.
Thanks for any answers
You should pay atention on the folowing docs Activity and Tasks. In short words: if user start app from recents you will receive onRestart before onStart (without onCreate it means that your app was just "suspended"). You able to save screen state using onSaveInstanceState(). But in general starting from icon and from recents - different application behaviors and you should provide proper code for this ways.
UPD
As described below root cause of unexpected behaviour was additional lunchmode attribute.
From what I experience as an Android user, both are same.
The difference we usually see is how we close the app
Press back button until app close / finish()
On this state no matter how we open the apps it will go to the main screen
Press Home button
On this state depend on the app. If the app does not handle any Activity the app will same with the first state. But if the app handle something like when onPause() the Activity then finish() the apps, then whatever you open with app icon or recent apps will provide the same result.
Correct me if I am wrong
I have successfully added admob ads to my android app and can view test ads on the emulator and my development phone (Nexus One). When I click on one of the test ads, it opens the web browser or market to that particular page(gmail or whatever). I click the home button to exit, but when I try to start my app again, it takes me back to the market page or browser that came up when clicking the test ad.
I have a feeling it is something in the onLeaveApplication or onPresentScreen methods implemented with AdListener, but I'm lost and the adMob documentation doesn't provide much info on this.
Generally this is the expected behavior for an application. If you click home and then back on your application icon, most applications will jump back to the last activity on the stack. It's certainly possible to kill your application altogether and treat re-entry as a brand new instance of your application, but you probably don't want that either as all user state will be lost unless you explicitly save it, which is even more work.
The AdMob client SDK listener callbacks are all optional for you to implement; there's no expectation that you do anything in particular. They're there as a convenience for you to pause, save, or resume whatever activity you were doing before.
Users are generally used to hitting the back button to go back to your app, not home and then back into the app.
I'm developing a Learning Application. In it, I have an Activity where the user can select some applications from a list of all the applications installed on his device.
Now, I'd like to launch my Activity whenever the user launches any of the selected applications from the app list. Basically I'd like to override the selected Activity by my activity. Once the user complete's some task, the user should be returned to the previously clicked Application.
How do I "Capture" this 'Launching other applications' part? BroadcastReceivers? Any example would be highly helpful. I'd be very grateful if anyone points me in the right direction with reference links.
This is very similar to a Lock Apps Application. But in a very badly twisted kind of way.
I know I have to use a background service to monitor the user activity.
You don't intercept arbitrary application launches, if that's what you're after. Doing this silently goes against the Android (or any reasonable) security model.
What you can do is offer an alternative Home screen.
However, if you just have a list view of available applications, nothing stops you from defining custom behaviours within that list activity.