I have a Firebase Recycler View which contains a button like this
final String post_key = getRef(position).getKey();
viewHolder.btnUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("post_key",post_key);
Log.d("post_key", data.getExtras().getString("post_key"));
data.setType("image/*");
data.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(data, GALLERY_REQUEST);
}
});
This will trigger an action in OnActivityResult(). I was trying to access the variable "post_key" in OnActivityResult. Therefore, I put it to the extra, and call the getString function in OnActivityResult.
final String post_key=data.getExtras().getString("post_key");
However, the app crashed and keeps telling me that I attempt to invoke virtual method 'getString' on a null object reference
You need to check the request code and the result code as well:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
// here you can access the intent
String msg = (data != null) ? data.getStringExtra("post_key") : "";
Log.d("post_key", "Got post_key: " + msg);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
You can not send your data to another app - if it does not accept. The thing you are doing is not mentioned anywhere.
Correct Way
in onClick don't pass extra, and hold your post_key in your activity/ adapter.
adapter.setPostKey(post_key);
Then in onActivityResult() get this post_key.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
String post_key = adapter.getPostKey();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Related
I am trying to launch an activity after a user has selected a photo. I was trying to do this:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
}
}
But I realised that with this code, the code for launching the activity (SendPhotoChangeActivity) executes before the user selects the image, crashing the app because the uriString variable is null.
I tried simply copy/pasting the code into onActivityResult(), but the view variable (in view.getContext()) was, of course, not recognized in onActivityResult().
I am thinking of simply replacing view.getContext() by getApplicationContext() in onActivityResult(). Is this the right thing to do? If not, please tell me how I can start an activity in onActivityResult().
If you are in Activity then you can just use this as Context
Intent goToActivityIntent = new Intent(this, SendPhotoChangeActivity.class);
If you are in a Fragment then you can obtain Context by calling getContext()
Intent goToActivityIntent = new Intent(getContext(), SendPhotoChangeActivity.class);
And use that code inside onActivityResult() as you were trying to.
set an integer code for the act of selecting a picture like REQUEST_CODE_TAKE_PICTURE so you know that has happened, this works ok in Kotlin, I assume that works as well with java:
if (requestCode == REQUEST_CODE_TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
Intent goToActivityIntent = new Intent(view.getContext(),SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
if (data == null) {
//Display an error
println("error accuered at onActivityResult ")
return
}
Have you tried this simpler one:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
startActivity(goToActivityIntent);
}
}
Just call
startActivity(goToActivityIntent);
to call the activity.
This assumes you are calling it from your activity or fragment. If this doesn't meet your requirements, let me know. There are other ways to implement this.
I have an activity that once a ToggleButton is clicked, it starts another activity as follows:
Intent intent = new Intent(ChatActivity.this, BBActivity.class);
startActivityForResult(intent,1);
In that second activity it performs few calculations and then:
confirmedData.putExtra( "confirmedB", (Serializable) bSelected );
confirmedData.putExtra( "dateInMillis",DateInMillis );
setResult(ChatActivity.RESULT_OK,confirmedData);
Then, I use the following to get the results in my first activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
ArrayList<DiscoverB> confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
Now, I had like to use confirmedB Once a button in my activity is clicked.
How can I pass the values from onActivityResult to my ClickListener?
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
HERE I NEED MY confirmedB
}
};
Thank you
In your first activity, declare a variable
ArrayList<DiscoverB> confirmedB;
In OnActivityResult, you can initialize confirmedB
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
You can use it now in your clickListener,
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
here you can use confirmedB
}
};
code in fragment
private static final int BRAINTREE_REQUEST_CODE = 777;
BottomMenu fragment = this;
public void onBraintreeSubmit(String clienTtoken) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(getActivity()), BRAINTREE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BRAINTREE_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
Toast.makeText(getActivity(), result.getPaymentMethodNonce().getNonce() + "", Toast.LENGTH_SHORT).show();
// use the result to update your UI and send the payment method nonce to your server
sendPaymentNonceToServer(result.getPaymentMethodNonce().getNonce());
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
}
}
}
code in activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Problem:
I have implemented BrainTree payment gateway with onBraintreeSubmit() method I startActivityResult and catch it onActivityResult but it's not being called in the fragment.
onActivityResult only gets called if I cancel payment but never when payment success or exception thrown
First you have to call from activity
Add below code in your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameLayout);
fragment.onActivityResult(requestCode, resultCode, data);
}
If you call startActivityForResult() with activity instance, then onActivityResult will get in your activity not in fragment. This is the reason for your issue.
Modified the code as below. This will solve your issue.
public void onBraintreeSubmit(String clienTtoken, BottomMenu context) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(context.getContext()), BRAINTREE_REQUEST_CODE);
}
not receiving callback in onActivityResult() after sending invite request to Google plus
public void sendGooglePlusInvite(){
Intent intent = new AppInviteInvitation.IntentBuilder(getActivity().getString(R.string.app_name))
.setMessage(getActivity().getString(R.string.invite_message_less))
.setCustomImage(Uri.parse(getActivity().getString(R.string.app_image_url)))
.build();
getActivity().startActivityForResult(intent, REQUEST_INVITE);
}
on call back in
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (resultCode == getActivity().RESULT_OK) {
if (requestCode == REQUEST_INVITE) {
// Get the invitation IDs of all sent messages
String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
for (String id : ids) {
Log.d("TAG", "onActivityResult: sent invitation " + id);
}
}
} else {
// Sending failed or it was canceled, show failure message to the user
// ...
}
}
Please help me
Yes, you can't receive callback by this way:
To get the result in your fragment make sure you call startActivityForResult(intent,REQUEST_INVITE); instead of getActivity().startActivityForResult(intent,REQUEST_INVITE); inside your fragment.
And secondly make sure you implement this method in your activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
this will call your fragment's onActivityResult()
Why my toast message is showing? My CEVAP_SORGULA variable equals 322, but my intents request code is 332
private final static int CEVAP_SORGULA = 322;
public void degistirActivity(final View view){
startActivityForResult(new Intent(this,veriTopla.class),332);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == Activity.RESULT_OK && requestCode == CEVAP_SORGULA ){
Toast.makeText(this, "Cevabin: " + data.getExtras().getInt("Cevap"),Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
I see clearly a type error between the CEVAP_SORGULA and the value passed via startActivityForResult. To fix it provide the variable instead of the hardcoded WRONG value.
startActivityForResult(new Intent(this,veriTopla.class),CEVAP_SORGULA);
you can try
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 332);
DEFAULT METHOD
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
SEE THIS EXAMPLE FROM ANDROID DEVELOPER SITE, MAKE YOU HELP
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}