I try onActivityResult in fragment currently I am using v4 fragment.
my activity code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v(tag, "0000");
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
My fragment code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v(getTag(), "in 0");
if (resultCode == Activity.RESULT_OK) {
Log.v(getTag(), "in 1");
if (requestCode == MY_INTENT_CLICK) {
Log.v(getTag(), "in 2");
if (null == data)
return;
Log.v(getTag(), "in 3");
Uri selectedImageUri = data.getData(); } } }
I call start Intent in
Log.v(getTag(), "in 00");
Intent intent = new Intent();
Log.v(getTag(), "in 01");
intent.setType("video/*");
Log.v(getTag(), "in 02");
intent.setAction(Intent.ACTION_GET_CONTENT);
Log.v(getTag(), "in 03");
fragment.startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
I also try call intent in
getActivity().startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
But noting is work. I also refer.
Fragment Compatability onActivityResult() Not Working
onActivityResult is not being called in Fragment
onActivityResult() not called in new nested fragment API
Refer below code
public class RegisterDeRegisterResponse {
private Vector<IResultResponse> _iCallBack = new Vector<IResultResponse>();
public static RegisterDeRegisterResponse ref = null;
public static RegisterDeRegisterResponse getInstance() {
if (ref == null) {
ref = new RegisterDeRegisterResponse();
}
return ref;
}
/**
* Register to recieve server response.
*/
public void registerForServerResponse(IResultResponse callback) {
_iCallBack.addElement(callback);
}
/**
* Notify all registered users about server response with corresponding
* process id.
*/
public void notifyRegisteredUser(int requestCode, int resultCode,
Intent data) {
Enumeration<IResultResponse> enumeration = _iCallBack.elements();
while (enumeration.hasMoreElements()) {
IResultResponse callback = enumeration.nextElement();
callback.onActivityResult(requestCode, resultCode, data);
}
}
/**
* Deregister to recieve server response
*/
public void deRegisterForServerResponse(IResultResponse callback) {
_iCallBack.removeElement(callback);
}
}
And also make Interface which have onActivityResult method declaration
public interface IResultResponse {
public void onActivityResult(int requestCode, int resultCode, Intent data);
}
Now in class in which you are calling startActivityForResult put this line before startActivityForResult and implement IresultResonse also
RegisterDeRegisterResponse register1 = RegisterDeRegisterResponse
.getInstance();
register1.registerForServerResponse(this);
and also deregister in onActivityResult
RegisterDeRegisterResponse
.getInstance().deRegisterForServerResponse((IResultResponse)this);
Related
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
}
};
Couldn't find a good answer for this. I have a class(no opened from MainActivity.
There I want to call startActivityForResult, to know when to do something on the UI.
How do I do it correctly? I passed the activity and context to the class.
On class:
private void init(){
Intent TestIntent = new Intent();
mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
}
On MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == TEST){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Check this, You have to pass code with Intent of Required Class
private void init(){
Intent TestIntent = new Intent(this,SecondClass);
startActivityForResult(TestIntent,222);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == 222){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Your TestIntent is empty. It doesn't have anything in it that tells Android what Activity to start. You need to create your Intent like this:
private void init(){
Intent TestIntent = new Intent(mActivity, ActivityToStart.class);
mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
}
private void init(){
Intent intent = new Intent(this,another.class);
startActivityForResult(intent,requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == reruestCode){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
private void getResponse(){
setResult(RESULT_OK,new Intent());
finish();
//Use this block of code in the second class.
}
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);
}
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);
}
I have a fragment within an activity and I am using a button to select a notification sound using the ringtone picker:
selectSound.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
if (notification != null) {
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) notification);
}
Log.v("TASKS", "Starting Ringtone picker for reminders");
startActivityForResult(intent, 5001);
}
});
Here's my onActivityResult in the fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v("TASKS", "onActivityResult in ReminderSettings: " + requestCode);
if (resultCode == Activity.RESULT_OK && requestCode == 5001)
{
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
SharedPreferences settings =
mActivity.getSharedPreferences("SETTINGS", Context.MODE_PRIVATE);
Editor editor = settings.edit();
if (uri != null)
{
editor.putString("NOTIFICATION_SOUND", uri.toString());
}
else
{
editor.putString("NOTIFICATION_SOUND", null);
}
editor.commit();
}
}
My activity has an onActivityResult but it does make call for unhandled results:
#Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
...
super.onActivityResult(requestCode, resultCode, data);
}
The strange thing is after launching and selecting a ringtone neither onActivityResult in the fragment or the activity are called when I debug and the activity seems to finish - there is no force close or exception on the Logcat. I have tested this now on 2 different devices and I get the same result - I'm using ActionBarSherlock for fragments if thats relevant.