Android Question How to return Calculator's result via intent

toby

Well-Known Member
Licensed User
Longtime User
The attached test B4XPage project opens the default calculator on a device, allowing the user to do some calculations. How to return the calculation result back to my app?

B4X:
Sub Button1_Click
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("openCalculator", Null)
End Sub

Inline java code in Main module:
#If JAVA
import android.content.Intent;
public void openCalculator() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
   }
#End If

TIA
 

Attachments

  • getCalculatorResult.zip
    16.4 KB · Views: 46

drgottjr

Expert
Licensed User
Longtime User
2 problems:
1) in order to receive a result back from calculator, you have to use StartActivityForResult(), not StartActivity().

2) just because you expect a result (or even wait for one) doesn't mean you're going to get one.
the activity you start(forresult) must be coded to call setResult(RESULT_SUCESS, intent_with_your_result).
i don't believe the stock calculator app does that. (just like android's stock translator does not return a
translation to a calling app.)

the most you could ever hope for might be, eg, if calculator allows you to set the result in the clipboard. in that
case, you could backkey out of calculator and return to your app and retrieve the result from the clipboard.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
@drgottjr,

Thank you very much for such detailed and insightful information. It certainly helps me understand the issue.
 
Upvote 0
Top