Comments (4)
You should use [fragment's startActivityForResult](https://developer.android.com/reference/android/app/Fragment.html#startActivityForResult%28android.content.Intent, int%29) and handle response in [fragment's onActivityResult](https://developer.android.com/reference/android/app/Fragment.html#onActivityResult%28int, int, android.content.Intent%29) method. Click here for more details.
Still, problem may occur if you use nested fragment API. Click here for more information.
from materialfilepicker.
Thank you for getting back to me. Before posting the issue, I had gone through similar questions on StackOverflow including the one you shared.
Based on those, this is what I was able to do so far -
(I) In my MainActivity, I have onActivityResult
call as follows -
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult triggered from Activity");
if (requestCode == 1 && resultCode == RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
Log.d(TAG, "filePath (from Activity) => " + filePath);
SettingsFragment.textDemo.setText(filePath);
}
}
(II) In SettingsFragment, declared the TextView as public static, because eventually, I need the Result inside that TextView only.
public static TextView textDemo;
(III) In SettingsFragment, I tried to call onActivityResult
as follows -
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult triggered (from Fragment)");
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
Log.d(TAG, "filePath (from Fragment) => " + filePath);
textDemo.setText(filePath);
}
}
From the logs, I found that no matter, how I modify (or shuffle) these codes, startActivityForResult
always triggers onActivityResult
from the Activity only, Never from the fragment.
As evident, by this method, I can get the output I wanted, but this does not appear to be the best/ideal way to do this. I have to be extra careful that this part of code never gets called when SettingsFragment is Not opened, as then, it would result in throwing up error due to using TextView textDemo
of SettingsFragment.
P.S. -
- In the Stackoverflow link you provided, one suggestion was to use
this
inonActivityResult
of the Fragment. But where can I use that? - Few of the answers suggested that this problem has been fixed in Android Support Library 23.2+, and I am using Android Support Library 23.4. So it should have been fixed. So is this the normal behaviour / method to call
onActivityResult
from Fragment?
How should I do this?
Thank you.
from materialfilepicker.
Hi. The problem may be in incorrect call of startActivityForResult method. In this case you have to call startActivityForResult
method of [Fragment](https://developer.android.com/reference/android/app/Fragment.html#startActivityForResult%28android.content.Intent, int%29) class, not method of Activity class: getActivity().startActivityForResult(...)
- wrong.
Also problem might occur if you are using MaterialFilePicker builder class (with withActivity
method). Starting activities from Fragment's using our builder class haven't supported yet. You should manually create Intent and start it using Fragment's startActivityForResult
method.
Please, show us how do you start FilePickerActivity.
from materialfilepicker.
OK. I was about to post the same. I was making a mistake of using MaterialFilePicker builder class from Fragment. After some trials and errors, I tried to test by making Intent and startActivityForResult
, it worked as expected. Seems like you had already posted about it,
Thanks for this library and your help.
from materialfilepicker.
Related Issues (20)
- getIntent() is now private? HOT 4
- Showing empty directory for android 10 HOT 4
- minSdkVersion is now 19 HOT 15
- Filtering Issue
- feature: hide "Add" button to be able to read last entry
- I would like to help the community
- This plugin is not working HOT 2
- Is it possible to remove the blue bar on the top , which shows the path ? HOT 3
- UnsupportedOperationException: Failed to resolve attribute at index 1: TypedValue{t=0x2/d=0x7f0300b7 a=7 r=0x7f050030 HOT 2
- Android 10 devices show white icons
- Library doesn't showing types other than images on Android 11 (like pdf, doc) HOT 2
- Change the sort order
- doesn't support androidx.fragment.app.Fragment HOT 2
- Android 11 Issue HOT 2
- JCenter is at end of life HOT 9
- Empty directories of files missing HOT 3
- Could not resolve com.nbsp:library:1.8 HOT 1
- Crash On Opening HOT 2
- Files (Pdf, doc, docx e.t.c) does not show up on Android 11, 12 and so on. HOT 3
- What's the current version?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from materialfilepicker.