I am trying to get result for dialer Intent using startActivityForResult()
Below is my code for Dialer Intent.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivityForResult(intent, 1234);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1234){
if (resultCode == Activity.RESULT_OK){
Toast.makeText(getApplicationContext(), "result ok", Toast.LENGTH_LONG).show();
}else if (resultCode == Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Result Cancelled", Toast.LENGTH_LONG).show();
}
}
}
whenever I am returning to my activity, Result Cancelled Toast is triggering.
Thanks in advance.
From doc:
ACTION_DIAL
public static final String ACTION_DIAL
You only have the ACTION. If you want to call a number from your application then you just have to put these lines of code into the onClick() method and can get what you want:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent); // no need to use startActivityResult(intent,1234)
Here, If an ACTION_DIAL input is nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: <yourURI> of an explicit phone number.
Additionally, there is no "Output" of RESULT_OK or RESULT_CANCELED, Cause, the startActivityResult() doesn't mean anything to ACTION_DIAL but startActivity(intent). Hope it helps.
why am I getting RESULT_CANCELED instead of RESULT_OK.
ACTION_DIAL does not return a result. If you read the documentation for ACTION_DIAL, you will see "Output: nothing". Hence, you will usually get RESULT_CANCELED. Only activities designed for use with startActivityForResult() will return a result code.
Related
I have 2 Activity classes and 1 Non-Activity class which calls startActivityForResult() from Context passed in constructor. This is how it looks: FirstActivity -> NonActivity -> SecondActivity -> FirstActivity. In SecondActivity there is ArrayList of custom objects that needs to be passed to FirstActivity as a result. There is a problem. When onActivityResult() is called resultCode is RESULT_CANCELED, but not RESULT_OK even if setResult(RESULT_OK, intent) is called. Here is my code:
NonActivity
public void showActivity() {
Intent intent = new Intent(request, ActivityKorak.class);
intent.putExtra("data", fields);
request.startActivityForResult(intent, 1);
}
SecondActivity
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("data", fields);
setResult(Activity.RESULT_OK, intent);
finish();
}
FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode != Activity.RESULT_CANCELED){
if(requestCode == 1) {
Bundle extras = intent.getExtras();
ArrayList<CustomInput> fields = (ArrayList<CustomInput>) extras.getSerializable("data");
}
}
}
You must simply remove
super.onBackPressed();
in the onBackPressed Method
What is happening is that "super.onBackPressed()" is setting the result code to "RESULT_CANCELED" and finishing your activity.
In my case, the problem was fixed by adding the SHA-1 and SHA-256 certificate fingerprints given in Android Studio (click on Gradle on the right side of the AS window, then run configurations and signingReport) to your Firebase project settings->General->SDK setup and configuration.
Update
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA
I've MyFragment that start the activityB like this :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this :
#Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment "); and it didn't get the RESULT_OK value and the Intent data is null, can someone please pointed out where did I go wrong? and how to pass the value from activity to fragment ? or maybe a better way instead of using onActivityResult?
You can try putting super.onBackPressed(); as the last line since RESULT_CANCELED is returned on back press.
Just startActivity by activity.
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment.
Scenario: I have a MainActivity.java, OtherPageForFragments.java and a fragment which is on OtherPageForFragments.java
In MainActivity.java, I have written the following code to start an activity and get result in
onActivityResult(int requestCode, int resultCode, Intent data)
is
startActivityForResult(new Intent(this, OtherPageForFragments.class),REQUEST_CODE_MAP);
In the onDestroy() of the fragment class, i have written this:
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
mlocManager.removeUpdates(this);
Intent intent = new Intent();
intent.putExtra("Latitude", passLatLng.latitude);
intent.putExtra("Longitude", passLatLng.longitude);
getActivity().setResult(Activity.RESULT_OK, intent);
getActivity().finish();
}
Now, I want my result in the MainActivity class. So, i have written the following code in the onActivityResult method:
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_MAP)
{
tost("2");
double lat=data.getExtras().getDouble("Latitude");
double lng=data.getExtras().getDouble("Longitude");
tost(lat + " -- " + lng);
}
The Problem: the resultCode getting returned is not Activity.RESULT_OK and the Intent I am getting is null.
What to do? Thanks
getActivity().setResult(Activity.RESULT_OK, intent);
getActivity().finish();
this code shouldn't be in onDestroy. onDestroy happens after the activity is already finished, and onActivityResult was called.
this code needs to in the code that closes the activity/fragment,
like on back key pressed, or a close button onClick
Try this:
Intent data = new Intent();
intent.putExtra("Latitude", passLatLng.latitude);
intent.putExtra("Longitude", passLatLng.longitude);
if (getParent() == null) {
setResult(Activity.RESULT_OK, data);
} else {
getParent().setResult(Activity.RESULT_OK, data);
}
getActivity().finish();
maybe you need clarify the launch modes for both activities. normally they should be "standard", if there is "singleTop" ... attributes in your activity manifest file. you need pay more attention.
For me removing flag Intent.FLAG_ACTIVITY_NEW_TASK for lauch intent resolved the issue
The method finish() is called before setResult()
Try overiding onBackPressed() method like this
override fun onBackPressed() {
setResult(RESULT_OK)
super.onBackPressed()
}
There are many question here from people having the exact same problem as I, and I've looked though a million, tried different things for 2-3 hours now, and I still can't get it working.
Child Activity:
Intent resultIntent = new Intent(myColorPicker.this, WidgetConfig.class);
resultIntent.putExtra("key", appwidget_notecolor);
setResult(RESULT_OK, resultIntent);
finish();
Parent Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Toast does not show
Toast.makeText(getApplicationContext(), "onActivityResult fired", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK ) {
// Toast does not show
Toast.makeText(getApplicationContext(), "Result Recieved", Toast.LENGTH_SHORT).show();
}
}
I launch child activity from parent activity like this:
Intent myColorPickerIntent = new Intent(WidgetConfig.this, myColorPicker.class);
myColorPickerIntent.putExtra("appwidget_notecolor", appwidget_notecolor);
WidgetConfig.this.startActivity(myColorPickerIntent);
Of course you won't get the result, you're calling startActivity() instead of startActivityForResult().
You don't seem to be calling startActivityForResult() after creating the Intent.
Are you pass the intent to startActivityForResult() method?
I created a 'New File' activity using
startActivityForResult(new Intent(MainActivity.this, NewFile.class),1);
The NewFile activity lets users set certain options for their text file then after clicking a button a string is saved to a static variable in my StringBuilder class and finish(); is called. How can I load this new string into the MainActivity's EditText? onCreate() is only called when the activity is first created right?
Do it on onResume or onActivityResult. It would be ideal though onActivityResult since you've used startActivityForResult, before finishing the other activity you set the setResult(int resultCode, Intent data) if you have intent to sent back or if none setResult(int resultCode). I think it is better to put the string which will be used to update your EditText as extra in the intent, then set the text using that string in onActivityResult method.
#Override
protected void onResume() {
super.onResume();
et.setText(DocumentBuilder.docText);
}
in your class NewFile.java :
String strName = "toto";
Intent intent = new Intent();
intent.putExtra("name", "toto");
setResult(1, intent);
finish();
in your MainActivity.java :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// Handle successful result
String name = intent.getStringExtra("name");
editText.setText(name);
}
}
}
refer this tutorial for more explanations