I have an android app chat. It has to be secure so I want to force my app to open always at Main Activity. This doesn't happen when I click the history of launcher, it opens the activity shown on launcher history not the main activity. How can I force it open the Main Activity always?
android:clearTaskOnLaunch="true"
I have this on Manifest but doesn't work.
that seems right and it should work but this option is a bit buggy when you build the app directly to the device, please try to install the apk manually
I offer you another way.
Look at the Application.ActivityLifecycleCallbacks, namely on :
onActivityPaused
onActivityResumed
onActivityCreated
When application was closed with HOME button or launch from recent task, you may have determine a boolean value that can be saved in SharedPreferences
boolean isSecure = false;
When activity paused - isSecure -> true -> save into sp.
When activity resumed\paused - read from sp -> isSecure = sp.getBoolean(...,false);
Related
I observed that if I open some apps installed in my phone via Google Play's 'Open Now' button the app will open as normal, if I navigate to another Activity inside the app, then pressed home, then go to phone's launcher, open the app through there. It will start a new task -- I believe -- because the App's launcher activity has resurfaced. If I press back, I will then be in the second Activity -- which is the last Activity in the foreground when I launched the app from Google Play. Is there a way to avoid this? I'm expecting to see the second activity in this scenario. I want to have the same task whether my app is launched from the Launcher or Google Play store.
I found out that a lot of apps in the Playstore has this behavior. One good example is the Zomato app and for reference my app is Shake Eat Off
I've been experimenting the android:launchMode in the Manifest with no luck.
I tried setting singleTask to the root activity and `singleTop to the second Activity
I also tried adding android:alwaysRetainTaskState="true" to the root Activity.
I also tried setting singleTask to the root Activity and singleInstance to the second Activity
So to explain more the chain:
Google Play App Page -> Open Now -> Root Activity -> Second Activity(now foreground).
Press home, Click App Icon.
At this point, The root Activity is now again showing (But I'm expecting to see the second Activity because it's the last activity on the foreground).
Press back ->
The Second Activity will resurface.
It happens because launcher intent is started again.
I see someone post a short solution in a discussion community. And I came up with a fully solution with the original solution.
In your splash screen you place these codes in onCreate()
if (!isTaskRoot()) {
finish();
} else {
// Place your current onCreate() codes
// We put code here because finish() doesn't kill the activity immediately while OS takes a while to kill it and we don't need codes to be execute
}
These code help you not skip new task and bring up all old created tasks
In your splash screen if there are some codes in onResume()
Place them in the following condition
if (isTaskRoot()) {
// Place your current onResume() codes
}
This help your onResume() codes do only when the task the old root task. Use this because finish() function in onCreate() doesn't kill the new Splash screen immediately while OS takes a while to kill it.
Why Android apps act differently when opening app from application icon and opening from Recent apps (after exiting application by pressing Home button)?
Example : Start Activity A -> Activity B then press home button.
Opening application from app icon will take you back to Activity A.
Opening application from recent apps will take you to Activity B.
Setting attribute "android:alwaysRetainTaskState" to "true" will solve this problem as this will retain the state of the activity as it is. It doesn't allow system to reset activity state.
"android:alwaysRetainTaskState
Whether or not the state of the task that the activity is in will always be maintained by the system — "true" if it will be, and "false" if the system is allowed to reset the task to its initial state in certain situations. The default value is "false". This attribute is meaningful only for the root activity of a task; it's ignored for all other activities."
My Android application(Application A) launches another application (say Application B) upon click of a button.
Now I want to implement "auto start" functionality wherein Application B will be launched as soon as Application A is launched.
For this, I created a checkbox and used SharedPreferences to store the value of checkbox.
Then, in my onCreate() method of Activity A, I am checking the value of checkbox from SharedPreferences and launching Application B in case the value is "true".
The Problem:
The problem I am facing is, when the user exits "Application B" (and comes back to Application A) the onCreate() of Application A is getting called again and Application B opens up again. This sets off a infinite loop and at exit of Application B, user returns to Application A and goes to Application B again.
I know onCreate() gets called multiple times (when we change orientation, keyboard opened, Activity goes into background and is killed by system), but is there any clean way of doing this?
To reiterate, my requirement is to launch Application B from Application A if the "auto start" checkbox is checked in Application A.
My suggestion will be to use the method onPause of the activity in application A and set a flag there "application B was called". Then if this flag is set do not call application B in onCreate of the activity in Application A and unset the flag.
If application B is too long in the foreground application A might be suspended by the system and the flag will be reset. In such case maybe it is good idea to have the flag stored in some persistent storage (e.g. SharedPreferences).
EDIT One more thing: the flag should be set in onPause only if the activity is being paused, because the other application will be shown (this will be easily determinable, because everything happens in the same class).
I have created a Android contact-book application. But when I enter the application, go through 3-4 screens and accordingly various activities are called. After which if I press the back button repetitively I return on first screen of the application.
If here I press back button some or the other previously visited screen is shown again, rather the application should either exit or should go in background.
Where have I missed out some standards that causes me to be stuck?
In short, the back-button on the first-screen of application shows irrelevant previously visited screens of the Android application. So I'm unable to move application in background or exit.
This issue can have these causes:
The application wasn't closed before
If you are developing with Eclipse and the emulator and old version of your application does not necessarily get killed if you fix something and test the application again. Eclipse only tells the emulator to start the main activity.
So your Activity stack possibly looked this:
Main Activity -> Activity A -> Activity B -> Activity A
thus pressing back from the last one will bring up the Activities in the order
Activity B -> Activity A -> Main Activity -> Home-Screen
if you do not exit the application and just let the emulator create the Main-Activity again this will happen:
Main Activity -> Activity A -> Activity B -> Activity A -> Main Activity
Thus any actions from that point on will extend the activity stack but does not necessarily clean the old entries out of your stack. (this will only happen if the system considers them not necessary anymore and kills them)
Overriden methods
It is possible (but unlikely) that you have caused this behaviour yourself.
If you did not override methods such as onPause() you most certainly did not cause this. Some apps are forcing the start of an activity in the onPause()-method (which is very bad style, since this is not expected behaviour). This could change your activity stack.
For testing purposes only, I am allowing my app APK to be downloaded and installed via a URL. Once downloaded on the phone, it can be launched with the Android app installer which gives the user an option to install it to their device and then run it.
Consider if we downloaded and ran the app in the way described above. The main/launcher activity in my app is a login page (Activity A). Once the user is authenticated, they are taken to the main area of the application, e.g. Activity B. So now the current activity stack of this task is A > B.
I then press the home button on the phone and am taken to the Android home screen. I re-launch my app via the icon in the menu, and I am taken to Activity A, instead of Activity B. Either the activity stack is now A > B > A, or there are now two separate tasks with activity stacks A > B, and A respectively. What I want is to be taken back to Activity B when I relaunch the app. Pressing back whilst in this state will take me back to Activity B.
This undesired behaviour only happens if I first open the app via the installer, and not if I open the app via the home screen/menu.
I looked into how the activities are being started by each mechanism. When we use the app installer, we see the following logs:
INFO/ActivityManager(XXXX): Starting activity: Intent { dat=file:///mnt/sdcard/download/[my app].apk cmp=com.android.packageinstaller/.InstallAppProgress (has extras) }
INFO/ActivityManager(XXXX): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=[my package]/[Activity A] }
via launcher / home screen:
INFO/ActivityManager(XXXX): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=[my package]/[Activity A] }
When started with the installer we see it is using the flag 0x10000000, but when started with the launcher we see it is using 0x10200000. It is also using an intent category.
From the docs we see the flags are:
public static final int FLAG_ACTIVITY_NEW_TASK
Constant Value: 268435456 (0x10000000)
public static final int FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
Constant Value: 2097152 (0x00200000)
The flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (which is being used when the app is launched from the launcher) seems to usually prevent a new task from being created if one already exists, and will restore the last used activity. This is the desired behaviour. Why is it not working in this situation? Is there anything I can do to make sure my application will always return me to the last Activity regardless of whether it was started through the app installer/launcher?
If I use singleTask it will always take me back to the main activity (Activity A) whenever I run the app (which is also not desirable).
Here is a question I found where someone is experiencing a similar problem (which has no accepted answer): App loses its ability to remember its stack when launched from another application
EDIT: Checking for the flag FLAG_ACTIVITY_BROUGHT_TO_FRONT in onCreate() of our launcher activity (and then finishing if it is set) seems to fix the main symptom, but clearly the underlying issue is still there. Is there a more complete fix?
EDIT2: The same result occurs when you download/run the app from the Android Market, so some of the above details may not be relevant.
Added the answer that antonyt provided:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
// Activity was brought to front and not created,
// Thus finishing this will get us to the last viewed activity
finish();
return;
}
// Regular activity creation code...
}
The underlying issue I believe is that the Intents used are different between the launcher and the installer. In so far as you are getting different Intent flags you are going to get different launch behavior. You can muck with the launch modes and you may be able to get a consistent result but fundamentally those different Intents will produce different results.
Your fix (or something like this) is probably your best bet.
Your problem is likely rooted in the fact that App installer doesn't use the LAUNCHER category, as does the launcher.
This bug has been documented elsewhere:
App always starts fresh from root activity instead of resuming background state (Known Bug)