2

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");
...
Simon
  • 1,691
  • 2
  • 13
  • 28

2 Answers2

3

You cannot pass a Drawable via a Bundle since it doesnt implement Serializable and I dont think you can implement Parcelable.

If its a Drawable thats already in your package you can just pass a String or Resource ID so you can look it up again.

If its Bitmap you will need to write it to local storage and pass the path.

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
  • Hi Cody, thx for the answer, to be more clear. It`s a drawable icon I grab from a .apk file on the internal storage. (The apk is NOT installed, just downloaded). I pass it like this "mydlModel.setImageId(info.applicationInfo.loadIcon(pm));" – Simon Nov 16 '17 at 19:22
0

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());
            }
        }
    }
Simon
  • 1,691
  • 2
  • 13
  • 28