Which one is better and why?
This one:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
...
}
or this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// do not call super.onActivityResult()
...
}
The first one is better.
It's more consistent with other event functions in the Activity API, it costs you nothing (the code you're calling does nothing at the moment), and it means you don't need to remember to add the call in the future when the behaviour of the base class changes.
Edit
As Su-Au Hwang has pointed out, my prediction about the behaviour of the base class changing in the future has come true! FragmentActivity requires you to call the method on super.
You should call super.onActivityResult if you are using FragmentActivity from the support package (also SherlockFragmentActivity). Otherwise it isn't necessary, however i'd just plug it in there for the sake of it. Check the source of FragmentActivity (no onActivityResult is not empty).
FragmentActivity source
Unless you have multiple subclasses of Activity which depend on it in your application, it doesn't look like calling super.onActivityResult() is needed, since the implementation of onActivityResult() is empty (I checked API level 15).
You can answer this yourself by looking at the source code for Activity.
Basically it's implementation of onActivityResult(...) looks like this...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
...so does nothing.
Although it seems the default implementation is empty, it's possible that in future updates that might not always be the case. I would recommend using it
Calling super is mandatory now. It throws "Overriding method should call super.onActivityResult" error. Adding super won't hurt your code.
Well First you need to understand onActivityResult is a callback when there is an action done with intent like choosing, clicking a photo, etc, and super().onActivityResult() returns an exception when the activity of intent gets crashed or something like that happens.
In short super().onActivityResult() is use to handle the error or exception during execution of Intent.
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (!mActivityResultRegistry.dispatchResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
Exception
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
throw new RuntimeException("Stub!");
}
Related
A URI is got through this known code in the following method as data.getData():
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 8010:
Log.i("Test", "Result URI " + data.getData());
break;
}
}
Now, there is another method (e.g. public void dosomething() {}) in the whole code where the URI is needed.
How can one get the URI, i.e. data.getData(), in dosomething()?
The use of data.getData() was tried in dosomething() without success. What works easily with standard variables normally, seems not to work with Uris.
ADDENDUM
Here a concrete example of code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dosomething() {
[--- here I need to get my data.getData() from onActivityResult() (directory chosen by user in startChoose()) --- something like: mypath = data.getData(); ---]
File imagedir = getExternalStoragePublicDirectory(mypath);
[...]
}
public void startChoose(View view) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "Choose directory"), 8010);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 8010:
Log.i("Test", "Result URI " + data.getData());
break;
}
}
}
User action:
A user choose a directory by clicking a button, which leads to startChoose() with onClick="startChoose" in the XML. onActivityResult() gives access to the chosen directory (=data.getData()) and this path shall be used in dosomething() but I don't succeed at getting the chosen directory (=data.getData()) in dosomething().
PS: getExternalStoragePublicDirectory() is deprecated but so far it can be used here (or you can update it as per API 29 if you want).
Basically, what I need is that the user chooses his/her directory by clicking on a button and based of that directory, the code continue its execution in dosomething().
Thanks in advance.
ok, after searching for a while, I found the following to transmit the Uri via methods:
The class must begin with this variable declaration:
public class MainActivity extends AppCompatActivity {
public Uri mypath;
onActivityResult can be like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 8010:
mypath=data.getData();
break;
}
}
And then, you can use everywhere in the code the variable mypath as following:
File mydirectory = getExternalStoragePublicDirectory(mypath.toString());
But mypath is the complete path of the directory and needs to be reduced to the environment path in order to work here. That's another game.
I have two activities:
ListViewActivity
AddActivity
and one object class :
todoObj
I have a list view in my ListViewActivity activity and when I click add button this will initiate AddActivity . In AddActivity when user enters Title, choose date, time and the category I want AddActivity to create a todo object and pass this back to the ListViewActivity.
Sorry I could not share the code itself, it always gives this error all the time so I uploaded on github please check it out.
Sorry again.
Many thanks.
All you have to fix onActivityResult method in ListViewActivity:
Because of you setting result code ADD_REQUEST_CODE in AddActivity "setResult(ListViewActivity.ADD_REQUEST_CODE, intent);", you should use "if (resultCode == ADD_REQUEST_CODE) {" in ListViewActivity not RESULT_OK.
You should receive intent from onActivityResult not ListViewActivity's intent. getIntent() gives ListViewActivity's intent. So use data variable:
onActivityResult(int requestCode, int resultCode, Intent data)
Final code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ADD_REQUEST_CODE) {
if (resultCode == ADD_REQUEST_CODE) {
Log.i("ListViewActivity", "Returned onActivityResult");
TodoObj todoObj = (TodoObj) data.getParcelableExtra("EXTRA_TODO");
Toast.makeText(ListViewActivity.this, "" + todoObj.getmYear(),
Toast.LENGTH_SHORT).show();
}
}
}
What is needed in the onActivityResult method after a purchase flow has been launched?
From the Trivial Drive sample:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (mHelper == null) return;
// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}
"here's where you'd perform any handling of activity results not related to in-app billing"
Does it mean you need to update the user's inventory or display an alert box? If so, I do this already in the OnConsumeFinishedListener. I have tested my code leaving the onActivityResult method as above and it seems fine. Could this potentially cause any problems?
Or does it mean I must manually call the consume method for the SKU purchased?
Your code is fine if you have not to handle other results in your activity.
Imagine an activity which e.g. start other activitys with startActivityForResult().
That's the place to handle those 'not related to in-app billing' results.
But then you should change the code to something like:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Pass on the activity result to the helper for handling
if (mHelper==null || !mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
super.onActivityResult(requestCode, resultCode, data);
}
So, I've read some threads about this, but I can't get this to work.
Basically I have this Dialog in which the user chooses to take a new pic or select a pic from their gallery. Here is the code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true)
.setItems(R.array.galeria_camera_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int userChoice) {
if (userChoice == 1) {
// take photo
}
if (userChoice == 0) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
}
}
});
return builder.create();
}
And then, the onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//choose file from gallery
}
Can anyone help? Before I have to re-read 10 pages worth of theory again... I'm quite new to this kind of things (onResult). Thank you.
Try implementing something like this in the activity where the dialog fragment is fired from:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.myFragmentId);
fragment.onActivityResult(requestCode, resultCode, data);
The underlining activity is what will receive onActivityResult, so u should be able to 'point' it towards onActivityResult of the dialog fragment.
The easy way to fix this is to move your fragment's onActivityResult into your activity, or you can use your activity's onActivityResult to call a method in your fragment.
I looked at my code and apparently I managed to get around this by calling this.getActivityForResult from my fragment, and then the result returned to my fragment, but I'll be honest with you: I don't remember writing this.
Ok, these two things made it work:
getActivity().startActivityForResult(galleryIntent, 1); You have to add getActivity()
As you guys said, you have to declare the onActivityResult() in the Activity itself.
Thanks guys!
I am creating a event using :
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
And invoking the intent using
startActivityForResult(intent, Constants.LINK_CALENDER);
There is nothing returned to call in
onActivityResult(int requestCode, int resultCode, Intent data)
It always returns data=null ie., returned intent result is always null.
I am trying to retrieve the "Done/Revert" state of calender event just created and some identity so that i can open the event again in future from elsewhere in code.
Are you sure that this activity does return something? Did not find definite proof that it should -or shouldn't- return anything, but that's what I would look up if I where you.
If it doesn't return anything, it's not shocking that you don't get anything as a returnvalue :)