简体   繁体   中英

How to pass data from Fragment to Activity

I want to pass Bitmap from fragment to the MainActivity. I want to save the bitmap ie in "image = a.getParcelable("BITMAP"); " on ActionBar button click. For that i need this image in my MainActivity. How can i do that?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // TODO Inflate the layout for this fragment

    setHasOptionsMenu(true);

    Bundle a = getArguments();

    image = a.getParcelable("BITMAP");

    View v = inflater.inflate(R.layout.main_layout, container, false);

    qImage = (ImageView) v.findViewById(R.id.qImage);

    qImage.setImageBitmap(image);

    return v;
}

In MainActivity

Bitmap a; 

In Fragment

MainAcivity main;
main = (MainActivity)getActivity();
main.a = image 

or call a method on the MainActivity to update Bitmap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM