Search code examples
androidandroid-fragmentsbundledrawable

Pass Drawable from Fragment to Dialogfragment using Bundle


I need to pass a drawable (mDLlist.get(position).getImageId()) from a Fragment to a DialogFragment.

I can`t seem to find a way how to do this, any input would be great.

Thank you upfront.

...
FragmentManager ft = ((FragmentActivity)context1).getSupportFragmentManager();
DialogFragment newFragment = MyNotification.newInstance();

Bundle args = new Bundle();
// How to pass this value ?
args.put?("appdraw", mDLlist.get(position).getImageId());
//--//
args.putString("appname", (String)mDLlist.get(position).getLabelnameText());
args.putString("appversion", mDLlist.get(position).getVersionName());
args.putString("appinstalltime", "Downloaded");
newFragment.setArguments(args);

newFragment.show(ft, "mydialog");
...

Solution

  • If anyone has the same problem, I used a "workaround" by passing the position of the adapter:

    args.putInt("appdrawpos", position);
    

    and compared it with a counter(i) which I use in a method I wrote:

    int appDrawPos = 0;
    Drawable drawIcon;
    int i = 0;
    ...
    //Get the adapter position
    appDrawPos = getArguments().getInt("appdrawpos");
    //Get apk icon
    getAPKicon(folder.getAbsolutePath());
    ...
    public void getAPKicon(String directoryName) {
            File directory = new File(directoryName);
            final PackageManager pm = getActivity().getPackageManager();
            File[] fList = directory.listFiles();
    
            for (File file : fList) {
                if (file.isFile()) {
                    //If counter is the same as adapter position
                    //Grab the icon from the apk
                    if (i == appDrawPos) 
                        if (file.getName().endsWith(".apk")) {
                            PackageInfo info = pm.getPackageArchiveInfo(file.getAbsolutePath(), 0);
                            info.applicationInfo.sourceDir = file.getAbsolutePath();
                            info.applicationInfo.publicSourceDir = file.getAbsolutePath();
                            drawIcon = (info.applicationInfo.loadIcon(pm));
                            appDrawPos = 0;
                            i = 0;
                        }
                        i++;
                } else if (file.isDirectory()) {
                    getAPKicon(file.getAbsolutePath());
                }
            }
        }