GithubHelp home page GithubHelp logo

Comments (4)

dimorinny avatar dimorinny commented on August 23, 2024

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.

atultiwari avatar atultiwari commented on August 23, 2024

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. -

  1. In the Stackoverflow link you provided, one suggestion was to use this in onActivityResult of the Fragment. But where can I use that?
  2. 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.

lukaville avatar lukaville commented on August 23, 2024

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.

atultiwari avatar atultiwari commented on August 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.