Android: Determine the previously displayed external Activity - android

In my application, I used a library that displays ads in my app. When a user clicks on the ad, it launches the Browser app, and the onClick events are handled by the library itself.
I also have a placed code in onPause() and onStart() methods that detects whether any of my Activities are sent to background (user pressed Home) or switching between any of my Activities.
So if the app is either freshly opened (no instance is running) or re-opened from background, it will display a dialog box. If the user is only switching from any of my Activites, then the dialog box shouldn't be displayed.
Now the problem is that when the user clicks on an Ad, the Browser app gets loaded and would mean that my App has been sent to background, and so when the user closes the Browser, it will still display the dialog box when it shouldn't.
Is it even possible for my App to determine the previously displayed external Activity (ie. Browser) and not display the dialog box?
Or are there better approaches that I can follow in implementing such setup?
Thanks in advance.

I don't think that there is a way for your Activity to know, which was the previous activity. A simple work-around is to save the time the dialog was displayed and not displayed it again, before some time passes. You can decide on the exact time period based on your application's requirements.
This may be in fact better in some situations. If the user switches from the Browser to your application and it has been some time he has used your application, it will be appropriate to show the dialog again.

you can use onResume() method to specify the behaviour when your activity get back from the background.

Related

Activity Lifecycle. When application already opened (Collapsed) is closing and starting again on icon click?

Can you please explain to me why my activity which is currently (onStop) is go to OnDestroy event and restarting again after clicking on the app icon in the interface.
Because of that i have problem with user data saving. I'm using event OnDestroy to ask user if he want to save the data. Because of my misunderstanding when i collapse the app and click to it again i am waiting to my activity to have all the data in it but instead i see saving dialog from the previous activity whiсh goes to onDestroy event.
Other apps don't act like this.
What should i check?
Your description is not very clear, so my answer may not help you.
Research the app life cycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your app will enter onStop is before onDestroy. This can happen if user activates another app. (So the user may no longer see any prompts that you display e.g. To ask to save or not.) Android may choose to destroy your app based on its rules after that point, even if the user clicks on the app icon to start it again.
Perhaps you can test what happens if the user switches back to the app (not clicking the icon), but use the "Recent apps" button and select it.

What is the difference between launch app from "recent apps" and tapping app icon

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

right way to add starting tips in Android app

I am developing an Android application, and I want to show some tips (few slides) for user when applicationis started first time.
I can make an activity and start it in OnCreate method of main activity, or make dialog window.
I want to ask: how to make it in the right way? Can experienced developers advise something, maybe with example?
You have to first make sure that these tips are launched only on first launch of app. I have seen this done by displaying a Dialog, and saving a value to shared preferences (or in sqlite for that matter). On next app launch this value is checked, if it is set, then you don't display the Dialogs.
This seems to be the simplest way. Note that when the app is uninstalled and installed by again the Dialogs will be displayed again.
Sometime we want to give the user ability to see this Dialogs once again. You can do this by setting/resetting the value in shared preferences from the apps settings screen. The user can change the value here and see the Dialogs again on app start.
Just make a Dialog Type Activity and show it automatically at the first time startup of your application.
and also add a help in you menu so that user can see it whenever he/she need help.

I am facing unexpected issue in android app

I have two activities: Login and List.
When i log-in i display the list. When user is in the list activity and press the home button. After that clicks on the app icon it brings to login page for some times only. While it should display the list.
How to resolve this?
Your app is pushed out of memory sometimes in order to free memory for other apps. When it happens, app is re-launched when user enters it and first Activity is opened. The only(or not the only) way for you to maintain your app state is to store it somewhere. You could save whether user logged in or not in SharedPreferences and when Login Activity is created you could check this.
Sometimes your app will be killed depending on how much memory other applications need. Your application must save and restore its state in order to behave as if it hadn't been killed.
If you read the section on the Activity Lifecycle in the developer docs, you should understand what you need to do.

How can I use back button while loading a page in an Android WebView?

I have a link in my application that triggers a webview to open from webview.loadUrl(...).
Sometimes it's takes quite a while to load the page, and in these cases I want to be able to use the built in back button on the phone to cancel the loading and go back to my application.
How do I do this? Is there some way I can listen to the backbutton while loading the url?
The back button is intended to move the user backwards through the activity stack.
I think it is possible you are handling too many user actions in a single Activity. As a general rule, any kind of user 'select' should trigger a new Activity.
Whenever the user does something that should be cancelable with the back button, you should trigger a new internal Activity via an Intent. Then the OS will give you this behavior automatically.

Categories

Resources