onActivityResult() called prematurely while calling Settings activity - android

I'm facing the known issue onActivityResult getting called immediately.
I read the reason behind it on some link
where it is advice to change launch mode of called activity.
But in my case I'm starting(navigating to) settings activity, to let the user enable data connection.
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS),
Constants.REQUEST_ENABLE_DATA_CONNECTION);
So I can't change launch mode of Settings activity.
I tried setting some flags to Intent e.g. intent.setFlags(0); but its not working.

I think there is some problem with your request code "Constants.REQUEST_ENABLE_DATA_CONNECTION".What is your request code value.
I think you should use zero "0" value like this.
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
but if you need to open wifi settings then use this code.
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
and see this Setting actions link for other setting actions.
I hope,this will help you.

Related

Return back to App after clicking on Accessibility Service

Please Please read the question complete before marking it as duplicate or vague
On clicking of a button I want to redirect the user to accessibility settings of the android mobile. Where user can click on the accessibility settings of the application. Here is the code that I am using for the same:
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(dummyIntent, 1);
Problem: When user clicks on, I want that it should redirect back to my application and should not remain on the accessibility screen itself.
It's quite late but i had to make the same stuff. So my suggestion is to use onServiceConnected() overridden method in class that extends AccessibilityService . When user apply accessibility in the settings you can launch intent to desired activity inside onServiceConnected(). But you should keep in mind that after device reboot onServiceConnected() also called, so use some flag to make sure that is not user action.
try this :
Intent dummyIntent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
ActivityContext.startActivityForResult(dummyIntent, 1);

Oncreate()is called after setResult instead of OnActivityForResult() in 2.2.1 LG-P500

In my application I use the startActivityForResult() and get the result using SetResult (request code,data). It is working fine, but in LG devices the setResult() is not getting called directly. First it is calling the onCreate() and then calling the onActivitForResult()
Below is my code
FirstActivity Button Click event
startActivityForResult(new Intent(TestActivity.this, Details.class),1);
call the onactivity result of first activity in second activity using below code
Intent data=new Intent();
data.putExtra("TEST", true);
setResult(2, data);
Details.this.finish();
Give me some suggestions for this.Thanks in advance
Please make sure u are not finishing the calling activity.......
it was crazy. I spend much for solving this issue and as result following fixed this issue:
turn off "Developer options" -> Apps -> "Don`t keep activities" option

Android Default application

i would like to know how to show
the check box that makes some application the
default in the createChooser dlg.
thanks in advance.
yes David i would like to know how to clear the app from being default. also i would like to know whether i can call any intent if i know its action, category, package and "class name", can I?
I actually tried to call some activity i previously known its intent contents put some SECURITY EXP. raised. why?
Your question isn't 100% clear to me, so forgive me if I'm answering the wrong question. It sounds like you want to either:
Pop up the dialog that lets the user select your activity as the default to handle some intent; or
Check if your activity is the default for an intent.
I'll answer both.
To pop up the dialog that lets the user select your activity as the default to handle some intent, simply call startActivity() with that intent. If for example you wanted to register yourself as the default home screen you would simply:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
1 of 3 things would happen.
Either there is no default specified (and if a user just installed your app, it will clear the default value) and thus the pop up will appear, or you're already the default or someone else is the default.
If you're already the default, then awesome, just figure out how to handle the intent this once (maybe add an EXTRA that explains that this was just a test so your activity knows to finish() immediately).
If someone else is the default you'll need to walk the user through clearing defaults for that app. If no one is the default you may want to throw up a Toast notification that explains what the user should do.
Toast.makeText(this,
"Please check 'Use by default for this action' and then press 'MyActivity'",
Toast.LENGTH_LONG).show();
These are all sort of messy situations to handle and it would be better to check first before calling startActivity(). To do that, use ResolveInfo.
ResolveInfo res = getPackageManager().resolveActivity(intent, 0);
// Got to be a better way to determine if there is no default...
if (res.activityInfo.packageName.equals("android")) {
// No default selected
} else if (res.activityInfo.packageName.equals(getPackageName())) {
// We are default
} else {
// Someone else is default
}
Hope this helps! If you want to know about how to make it easy for the user to clear another app from being default, let me know.

Bluetooth activation Alert dialog multiplication after screen rotation

I have encountered strange issue. From Activity onStart() I request Bluetooth activation and 120s discoverability via intent:
Intent activateBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(activateBTIntent, BT_ACTIVATE_INTENT);
I use it no matter if the Bluetooth is already activated or not. Android documentation says that Bluetooth will be activated if it was not, and that works fine. In both cases I get system Alert dialog
When I rotate the screen I observe flickering. Press on Yes/No removes one dialog, but there is still another one below. Performing screen rotation I can get a pile of Alert dialogs, and have to press Yes/No on each to get rid of them.
Described issue is present only if Bluetooth was not already started when intent was sent, otherwise it works correctly. Tried on different 2.2 phones, and issue is present on all. Looks to me like Android system issue.
Has anybody encountered it also, and maybe have some useful hint how to avoid this?
Thanks and regards.
This is a bug in the Settings app that causes this. The RequestPermissionActivity is duplicating its instance of RequestPermissionHelperActivity on rotations.
I was facing the same problem. After spending lots of time with it, i have found one solution.
You can avoid the duplication of the 'Bluetooth activation Alert dialog' by using
FLAG_ACTIVITY_NO_HISTORY flag for activateBTIntent. But setting this flag will make startActivityForResult unusable.
The code -
Intent activateBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
activateBTIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(activateBTIntent);
Using FLAG_ACTIVITY_NO_HISTORY with activateBTIntent will remove the 'Bluetooth activation Alert dialog' activity from the history stack. Thus in this case, when orientation is changed, the 'Bluetooth activation Alert dialog' activity will be finished and newel created(NOT DUPLICATED):
Reference
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NO_HISTORY
(Your post doesn't nearly have enough info, so I'm assuming here...)
I'm not too sure about the startActivityForResult.
You shouldn't do showDialog() in onCreate() if you use a managed AlertDialog. If the Bluetooth thingy does that, could you try not running the above code from onCreate()?
The issue has been reported (google issue tracker) but it is already open after 6 years (checked with Android 7.1.2).
Unfortunately the flag FLAG_ACTIVITY_NO_HISTORY does not work anymore. The only quick workaround I found until Android team fix this (if they ever will) is to lock activity screen orientation change, just for the time the dialog is displayed:
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
to be called just before
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
and then use
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
super.onActivityResult(requestCode, resultCode, data);
}
on intent result.
It's a temporary remedy but usually better than showing this possible issue during BT activation.

How i can close/exit button on my app?

i have a button to close my app with this code:
finish();
the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....
how i can do a real exit/close button?
i tryed with this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app
If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.
Incidentally I'm writing this from memory. function names may not be exact.
Hope that helps.
p.s. re-read your requirements. you can always stop the finish loop at your first activity.
I would do it this way:
I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
I would then invoke my MainMenu from the activity that is going to close the application.
startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);
Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();
Haven't tried it but I think it should work.
I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Chances are, you don't want an exit button. Perhaps a logout button, but that's it.
finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.
you can try this: System.exit(0);

Categories

Resources