Android application does not relaunch when navigating to the home screen - android

We have a launcher application that works fine on older versions of Android. We have a device that is running Android 5.1, and are running into issues.
When pressing the back button from within the application, we allow the user to go to the settings page. Pressing the home key re-launches the application. Pressing the back button on other devices also relaunches the application.
On the new device, pressing the back button allows us to navigate to the Android home page. It does not launch the application.
We are overriding the back button like so:
#override
public void onBackPressed() {
// Display the password prompt if required
if (PreferencesManager.isPasswordPresent()) {
LeaveApplicationPasswordDialogFragment dialog = LeaveApplicationPasswordDialogFragment.getInstance();
dialog.show(getSupportFragmentManager(), "password");
}
else {
// Prompt whether we are about to leave the app
LeaveApplicationDialogFragment dialog = null;
MyApplication application = (MyApplication )
getApplication();
if (application.isDefaultLauncher()) {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ** to access the device's settings?");
}
else {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ***");
}
dialog.show(getFragmentManager(), "leaving");
}
}
In the dialog fragment, we accept the confirmation and process it like so:
public void exitToSettings() {
GUIAndroidTouchBaseActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
shutdownOperations();
finish();
}
Per some research and other threads, I worked with our exit method like so:
public void exitToSettings() {
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
GUIAndroidTouchBaseActivity.this.startActivity(intent);
shutdownOperations();
finish();
}
No dice. Same behavior.
What am I missing? Is there something in OS 5.1 that's overriding our launcher? Again, pressing the home button launches the app as expected. Navigating to the home page from the settings page by pressing the back button does not.
What we have works on other devices and OSs. We've had no issue with 4.1 and 6.1.
We are also overriding the back button like so:
#Override
public boolean onKeyDown(int keyCode, KeyEvent KEvent) {
int deviceid = KEvent.getDeviceId();
//Making sure not processing same key again
if (KEvent.getRepeatCount() != 0) {
return true;
}
if (!SettingsOpened) {
int keyaction = KEvent.getAction();
// "Esc" key can not be stooped id diveceid is non zero because it can be back key of android
if (KEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && deviceid != 0) {
return super.onKeyDown(keyCode, KEvent);
}
if (keyaction == KeyEvent.ACTION_DOWN) {
String key = KeyEvent.keyCodeToString(keyCode); //wont work in version 11 or less
if (keyCode != KeyEvent.KEYCODE_ENVELOPE) {
Matcher matcher = KEYCODE_PATTERN.matcher(key);
if (matcher.matches() || ExternalKeyboard.keyMatches(KEvent)) {
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState());
char character = (char) keyunicode;
//toast.makeText(this, "onKeyDown" + _lastChar + repeatcount, toast.LENGTH_SHORT).show();
_lastChar = character;
_actionDown = true;
ExternalKeyboard.KeyboardAddChar(character);
}
}
}
return true;
}
else {
return super.onKeyDown(keyCode, KEvent);
}
}
Thanks!

Adding
android:stateNotNeeded="true"
android:clearTaskOnLaunch="false"
to my manifest took care of it.

Related

Avoid an item from Action Bar from being double clicked

I have designed an action bar for my Android app. In this action bar there's a button that launches a Dialog Activity used to configure my app's behavior. If I double click this button fast enough, I'm able to order the Dialog Activity to be launched twice before it actually appears, and then it appears duplicated and visually overlapped and I don't want this. I tried to create some sort of lock-down mechanism but it is not working because my Dialog Activity is launched only after all the code in my Main Activity calling method (onOptionsItemSelected) is executed. Is there a way to avoid this form happening?
My code is:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//ensure only one element from the option menu is launched at once (if you double click fast you could launch two)
Log.e("test", "onOptionsItemSelected ");
if(optionItemAlreadySelected == false)
{
optionItemAlreadySelected = true;
int id = item.getItemId();
if (id == R.id.action_sound_mode) {
//item.setVisible(false);
Intent intent = new Intent(this, SoundConfigurationActivity.class);
startActivity(intent);
optionItemAlreadySelected = false; //this code is executed before the activity is started!!!
return true;
}
}
return super.onOptionsItemSelected(item);
}
Is there a way to know when the Dialog Activity has already being closed and lock the opportunity to open it once again until then.
Kotlin
It's a screen(Activity, Fragment) based solution to avoid a double tap on menu action.
Add below global variable to your activity/fragment containing onOptionsItemSelected function.
private var previousClickTimeMillis = 0L
Write below function anywhere in the project i.e Utils.
fun singleSafeClick(
previousClickTimeMillis: Long,
block: (previousClickTimeMillis: Long) -> Unit) {
val currentTimeMillis = System.currentTimeMillis()
if (currentTimeMillis < previousClickTimeMillis || currentTimeMillis >= previousClickTimeMillis + OnSingleClickListener.DELAY_MILLIS) {
block(currentTimeMillis)
}
}
Write your triggering code as below.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_delete -> {
singleSafeClick(previousClickTimeMillis) { tappedTime ->
previousClickTimeMillis = tappedTime
// Write Yyur code here
}
}
}
}
You can use a boolean variable to track the state of your Dialog. When you click the button you set mDialogShown = true to block any other show dialog requests.
Now when the user presses Back button and the Dialog is closed onActivityResult is called.
At this point your are sure that the Dialog was closed.
I assumed your code is inside an Activity:
class MainActivity extend Activity {
static final int SHOW_DIALOG_REQUEST = 1; // The request code
static boolean mDialogShown = false; // True if dialog is currently shown
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_sound_mode) {
showDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private void showDialog() {
if (!mDialogShown) {
mDialogShown = true;
Intent intent = new Intent(this, SoundConfigurationActivity.class);
startActivityForResult(intent, SHOW_DIALOG_REQUEST);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == SHOW_DIALOG_REQUEST) {
mDialogShown = false;
}
}
}
Documentation
https://developer.android.com/training/basics/intents/result.html
https://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog

Disable Back button in WizarDroid

I am using WizarDroid library, and creating a simple Wizard.
What I am trying to do is at the final step I want to disable Back button at the bottom. I have searched every where and the documentation is not helping me.
Here is my Basic Wizard code:
public class FormWizard extends BasicWizardLayout
{
public FormWizard()
{
super();
}
#Override
public WizardFlow onSetup()
{
return new WizardFlow.Builder()
/*
* Mark this step as 'required', preventing the user from advancing to the
* next step without selecting one option.
*/
.addStep(Form1.class, true)
.addStep(Form2.class, true)
.addStep(Form3.class)
.create();
}
#Override
public void onWizardComplete()
{
super.onWizardComplete();
// Terminate the wizard
getActivity().finish();
}
}
The Form1,Form2 and Form3 are simply extending WizardStep and showing some data to user.
In the documentation the wizard.goNext(); method is defined but it is not available in my scenario.
I simply want to disable the user to go back after they reach at Form3 or final step.
If you are talking about disabling the software/hardware (next to the home button and menu button) then you simply have to handle the onPress yourself. Example:
//FOR API 5-
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "Can't go back", Toast.LENGTH_LONG).show();
}
//FOR API 6+
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(getApplicationContext(), "Can't go back", Toast.LENGTH_LONG).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}

Launching app from home screen will launch it on top of an existing instance if it was launched previously from the app screen, vise versa

There seems to be a bug of some sort with the play store installer, whenever I try to resume my app from the home screen icon rather than the app screen it will launch again on top of my already running app. This is true for the other way around
I've tried this code
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& intentAction != null
&& intentAction.equals(Intent.ACTION_MAIN)) {
finish();
}
}
But all it does it crash the app if it tries launching in the fashion I stated.
My manifest is also set to
android:launchMode="singleTask"
I found that if I take exactly the same APK and install it using "adb install", my app works correctly and as I expect.
However, if I (or my users) download the apk and install it from the Downloads, I find the behaviour described above, namely a new instance of my Activity being created on the stack when the user navigates to home and then back to the app via the launcher. This can be verified with "adb shell dumpsys activity "
I fixed the issue by using the following at my Launch Activity
ublic class StartupActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (needStartApp()) {
Intent i = new Intent(StartupActivity.this, GameActivity.class);
startActivity(i);
}
finish();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// this prevents StartupActivity recreation on Configuration changes
// (device orientation changes or hardware keyboard open/close).
// just do nothing on these changes:
super.onConfigurationChanged(null);
}
private boolean needStartApp() {
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);
if (!tasksInfo.isEmpty()) {
final String ourAppPackageName = getPackageName();
RunningTaskInfo taskInfo;
final int size = tasksInfo.size();
for (int i = 0; i < size; i++) {
taskInfo = tasksInfo.get(i);
if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
// continue application start only if there is the only Activity in the task
// (BTW in this case this is the StartupActivity)
return taskInfo.numActivities == 1;
}
}
}
return true;
}
}

android webview history does not work properly

My application loads a url of a website into an android webview.
This website consists of 3 pages (actually its the same page with different ID)
lets call them A B & C.
The website, has a back button, which code is:
javascript:history.go(-1)
If i browse from A to B and presses the back button - it works fine.
If I browse from B to C and presses the back button - I get back to B - also fine.
If I then press the back button, I get back to C instead of getting to A - NOT fine.
Then If i press back I will get back to B...back again to C...and then to B...endlessly, I never get to A.
If I use the physical back of the device and use the following, it works as expected:
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
Extra info:
I am using a WebViewClient, override "shouldOverrideUrlLoading" and enable javascript:
webView.getSettings().setJavaScriptEnabled(true);
Any ideas?
Thanks!
I write code like this
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:") || url.startsWith("mailto:")) {
return true;
}
if (mOnWebViewEventListener != null) {
mOnWebViewEventListener.onWebViewPageStart(view);
}
if (mShowProgress) {
mProgressBar.show();
}
return false;
}
It's work or not?

How to distinguish an Importand Application from a non Importand one when both are in background

I hope that this question will not have the same luck with this one.
I want to give my users the ability to close some applications which are in the background via a list. By applications in the background I mean applications that the user started and then press the home button, for example Internet Browser. I have managed to find the background applications via the ActivityManager (both getRunningAppProcesses and getRunningTasks will do the job). However there are some applications in the background which are important for the system to work (i.e. Phone, Launcher, Input Methods etc) and I don't want them in my list.
My question is: How can I distinguish them from the non Important ones. I don't want to use some kind of String checking / filtering (like contains(com.android) etc) because I want to exclude some Important 3rd party apps like non stock Launchers, Dialer, Messaging etc.
Everyone who owns a Galaxy S2 and have used the "Program Monitor Widget" will know what i mean.
Thank you very much for your time and efforts...
Well I finally came up with a function that checks if a running task may considered as an "Important" one. First of all we have to use the getRunningTasks function from the ActivityManager to get a list filled with ActivityManager.RunningTaskInfo objects and then we pass each of these objects to the following function to do the check. Hope this helps someone...
public boolean isRunningTaskImportant(ActivityManager.RunningTaskInfo taskinfo) {
//What makes a running task important is somehow "fluid" but there are a few task categories
//on which is safe to assume that they are important. These categories are:
//1. If task has not any actual activities running. This is because these are not actuall running but they are frozen by the
// the system and they will be killed if needed. They are not Important but we do not want them in our running apps list.
//2. Well known namespaces including our own if we want to
//3. Home Launcher Applications
//4. Phone Handling Applications
boolean result = false;
ComponentName bActivity = taskinfo.baseActivity;
if (bActivity == null) return false; //<-- The task has no base activity so we ignore it...
String pName = bActivity.getPackageName();
if (taskinfo.numRunning == 0) {
result = true;
} else {
if (pName.equalsIgnoreCase("com.android.phone")) {
result = true;
} else if (pName.equalsIgnoreCase("com.android.contacts")) {
result = true;
} else if (pName.equalsIgnoreCase("com.chdcomputers.powerpanel")) {
result = true;
} else {
//Here we are checking if out task is a home launcher application.
//This code is based on this question: http://stackoverflow.com/questions/3293253/getting-list-of-installed-apps-easy-but-how-to-launch-one-of-them
Log.d(TAG, "isRunningTaskImportant checking for launchers");
Intent launchersIntent = new Intent(Intent.ACTION_MAIN, null);
launchersIntent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> list = cx.getPackageManager().queryIntentActivities(launchersIntent,0);
boolean found = false;
for (ResolveInfo ri : list){
if (!found){
Log.d(TAG, "isRunningTaskImportant checking launcher app: " + ri.activityInfo.applicationInfo.packageName);
found = pName.equalsIgnoreCase(ri.activityInfo.applicationInfo.packageName);
}
if (found) break;
}
result = found;
if (!found) {
//Finaly we are going to check if out task is a Phone Handling application
//The idea behind that is to check for what kind of permissions the application wants
//In my opinion any "serious" phone handling app should ask for, at least, the following 9 permissions:
//CALL_PHONE, CALL_PRIVILEGED, READ_CONTACTS, WRITE_CONTACTS, SYSTEM_ALERT_WINDOW, READ_PHONE_STATE,
//MODIFY_PHONE_STATE, PROCESS_OUTGOING_CALLS, RECEIVE_BOOT_COMPLETED
//So if the application asks for those permissions we can assume that is a phone handling application...
Log.d(TAG, "isRunningTaskImportant checking possible phone app: " + pName);
try {
PackageInfo pi = cx.getPackageManager().getPackageInfo(pName,PackageManager.GET_PERMISSIONS);
String[] perms = pi.requestedPermissions;
if (perms == null) {
result = false;
} else {
int pCount = 0;
for (String perm : perms) {
if (perm.equalsIgnoreCase("android.permission.CALL_PHONE")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.CALL_PRIVILEGED")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.READ_CONTACTS")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.WRITE_CONTACTS")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.SYSTEM_ALERT_WINDOW")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.READ_PHONE_STATE")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.MODIFY_PHONE_STATE")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.PROCESS_OUTGOING_CALLS")) {
pCount++;
} else if (perm.equalsIgnoreCase("android.permission.RECEIVE_BOOT_COMPLETED")) {
pCount++;
}
}
result = (pCount == 9);
}
} catch (Exception ex) {
Log.e(TAG, "isRunningTaskImportant checking possible phone app ERROR: " + ex.getMessage());
result = false;
}
}
}
}
return result;
}

Categories

Resources