I'm working on an android app that can call upon HP ePrint app to perform printing wirelessly. The printing function have been tested working. However, after the user press the print button, i would like my app to come to the front instead of it staying at the ePrint app, and for that I'll need the response code from the ePrint app.
I googled that one way to do it is by calling the intent with startActivityForResult, as:
startActivityForResult(intent, REQUEST_CODE_PRINT_FILE);
however i failed to find the value for the variable REQUEST_CODE_PRINT_FILE. Do i need to know the value of the variable or i can simply use RESULT_OK in my case?
Thanx.
REQUEST_CODE_PRINT_FILE can be a any int value >= 0 , this value will be returned back to you onActivityResult() arguments once the printer Activity destroyed.
please check the docs.
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29
Related
I am wondering about the following.
In the Android documentation they recommend the following:
"Tip: When your configuration Activity first opens, set the Activity result to RESULT_CANCELED, along with EXTRA_APPWIDGET_ID, as shown in step 5 above. This way, if the user backs-out of the Activity before reaching the end, the App Widget host is notified that the configuration was cancelled and the App Widget will not be added."
https://developer.android.com/guide/topics/appwidgets/index.html#Configuring
But isn't that redundant, since the default value is RESULT_CANCELED (0) anways? Am I missing something? Can there be cases where the result is not at 0 when we open the configuration activity?
The important part of that statement is "along with EXTRA_APPWIDGET_ID". You're correct that the result code will be RESULT_CANCELED by default, but there isn't going to be a result Intent with the Widget ID attached by default.
Certainly, any launcher that allows Widgets should be able to handle it gracefully if that Intent is not set, but it's a known issue that at least some do not.
When using the intended(IntentMatcher) from the Android Espresso API, is it possible to do this twice in the same Activity?
So for example I click a button which starts an Activity for result. I check that this Activity has fired using intended(IntentMatcher). That works.
However, when I get the result, I want to fire off an Intent for a different Activity. In this case just a local Activity in the same app package with no result.
When I do this manually in the app it works fine, but Espresso can't seem to detect the second Intent in my test. What I am I missing or is this not possible? Alternatively, how should I be doing it? Maybe my design is bad.
When I do the check I'm trying this:
intended(toPackage(<packageName>));
intended(hasComponent(hasClassName(<className>)));
The first line matches but not the second one. And even if the second line is not completely correct it never seems to show anything in the error log about the second Activity I'm actually starting.
Another thing adding to the confusion is that two intents are definitely being fired as it shows that in the log. They both seem to be the same one but with slightly different details - one is a package, one is a component. Does it log the result from the first Intent as an Intent in itself? Sounds unlikely but where is this other intent coming from? I know it's not the second Activity I'm launching as it still fires even when that Activity isn't called (when the first Intent result is a fail).
I've also considered that maybe it's not getting detected because it's not waiting long enough for the second intent to fire. If that were the case, what would I do about that? I don't see much talk about handling time sensitive things in Espresso. Like checking if a progress bar is shown but then hidden again while not pressing anything. How do you do that? Maybe it's the same answer.
Any help appreciated!
Ok I found the problem. My IdlingResource wasn't working.
After fixing that it works like a charm :)
enter image description here
When I call the system camera through the above codeļ¼the onActivityResult method does not perform on some phones,For example, Samsung S4,That's why.Ask for help
There are 2 possible solutions:
You should try with request code larger than 0.
Does your activity contain singleInstance or singleTask launchMode? check this issue .
next time please add the source code. its easier that way.
and the logcat if some error is there.
but it seems that what you want to achieve is something like
Android ACTION_IMAGE_CAPTURE Intent
I wondered if anyone can shed some light on this,
I have one activity app which has a listview. In onCreate() I populate some data in that listview (reading it from sqlite database). I have a close button which triggers finish(); too. now when I press close button, activity disappears but when I click on app icon on desktop (or selecting it from phone history button) I see all previous data in the listview. the function that I am looking for is to start app fresh after clicking close button for next run. (something like banking app log-out button). (list view here is only an example to put across the need, so clearing list-view before finish(); is not acceptable. It is very simple and plain request and I do not think any code is necessary but if anyone interested I will post some code too.
What I want is same behavior as a banking app in exit, when user leave the main screen or click sign out, the App closes altogether. I can achieve this by using following methods (number 2 and 3) but apparently these solutions are not best practices. Then what method a banking App uses to close the app instantly? I doubt they use something which is not best practice such as System.exit(0)?! or do they!
Many developers claiming closing an App or killing parent activity
is OS job
Some say use use :
int pid = android.as.Process.myPid();
android.os.Process.killProcess(pid);
(this solution according to this is not a good idea because in next run app acts like it has been crashed last time https://stackoverflow.com/a/24015569/4173238 )
some say use System.exit(0); according to this
https://stackoverflow.com/a/5846275/4173238 is not recommended either
some say use finish(); but finish does not do what I want
Thanks for any input
If you have a mechanism that allows it so that you only deliver messages to the main thread when the application is resumed, then you can register your activities on an event bus like Otto and send an event that every Activity is subscribed to on which they call finish() on themselves.
Another possible solution is starting every activity with startActivityForResult(), and if you click the exit button, then you would say
public static final int KILL_ACTIVITY_RESULT_CODE = 0xD34DB33F; //why not
public boolean onOptionsMenuItemSelected(MenuItem menuItem) {
if(menuItem.getId() == R.menu.kill_activity) {
setResult(KILL_ACTIVITY_RESULT_CODE);
finish();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == KILL_ACTIVITY_RESULT_CODE) {
setResult(KILL_ACTIVITY_RESULT_CODE);
finish();
}
}
}
...and one time I've seen someone make static references to every single activity they had, and called finish() on each and every one of them. But don't do that, that essentially means you have failed as an Android programmer, and there is nothing to redeem you of your sins.
As brilliant CommonsWare, has pointed out in his comment "Static" was the issue! I was using static variables to store data to fill My listView. Apparently even if you have only one Activity and close it, Static variables remain intact! on app re run!
If you asking why I used static variable at the first place, I have to say, right or wrong, I wanted to share that variable between my other java class (my databaseHandler.class).
Why Android not clear all (including static variables) resources when closing the main and only Activity of app, remains a question and this is my next reading topic! but many thanks for anyone who post a comment on this question,
I will also change the question from:
How Banking Apps close? finish() does not do the same job for me
to
closing an activity using finish(); wont make app start fresh in next
run! why?
My simple structure:
List.java (Activity A)
New.java (Activity B)
Steps:
Start A and use startActivityForResult() to turn to B by a button.
Then in B, use Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); and startActivityForResult() to take a photo by call up camera.
After that, onActivityResult() in B is called. Then I click a button to set a resultcode and close B.
However, B is closed, but onActivityResult() in A is never called.
If I skip the step to take a photo, click a button to set a resultcode and close B. onActivityResult() in A will be successfully called.
I'm just confused about these two situations. Please anyone tell me why the first situation happens?
Thank you very much.
its happened when your first Activity would be destroy..
I tried to replicate what you are describing but with no luck:
http://androidgecko.com/project.zip
Have a look and tell me if I missed something.
Know that when another application is going to the front (camera in this case) android might kill previous processes to free memory (not even activities as google docs describe, but whole PID) which might cause your problems.
I have seen this behaviour especially on older phones when you launch Gallery or Camera applications from your application.
This is one of the reasons why Applications like Facebook have their own implementation of Gallery/Camera (so that PID is not killed because its the front one).
However.. even if PID is killed what you should see is Activity recreation, meaning its onCreate with savedInstanceState not being null should fire.. and then onActivityResult..
check out my example and let me know how I could reproduce your issue.