vendredi 13 novembre 2015

Fragments impede startActivityForResult and I don't know why?

here is my tricky situation. I could not find help in any other posts because of the complexity of the framework of my project.

I have a main activity (MainActivity.class) in which I implemented a Navigation Drawer whose options lead to various Fragments (FirstFragment. class, SecondFragment.class and so on).

Now, I successfully managed to set the FirstFragment's layout to appear right away on the screen and by clicking on some Views inside this Fragment I am opening various Popup Activities (Popup1.class, Popup2.class and so forth).

What I would like to do now is the following (and it is normally working).

By clicking on some CheckBoxes in the Popup activities, I am generating a String that is then shot to the Fragment and can be later shared with a SHAREintent from the navigation drawer. e.g. if you click on checkbox1, the final string will be "A", if you click on Checkbox2, the final string will be "B" and if you click on both the final String will be "AB". (I am using an arraylist+append+toString method.)

Therefore, I implemented a startActivityForResult method when switching from the FirstFragment.class to the Popup.class so that thefinal String can be shifted to the Fragment and the WORKING codes are the following:

Intent Open1 = new Intent(getActivity(), Popup1.class);
                            startActivityForResult(Open1, 0);

(...)

Intent Open2 = new Intent(getActivity(), Popup2.class);
                            startActivityForResult(Open2, 1);

(...)

Intent Open3 = new Intent(getActivity(), Popup3.class);
                            startActivityForResult(Open3, 2);

etc to open the Popup

+

the following code in the Popup1.class to send back some data (similar one used in Popup2.class etc)

@Override
public void finish() {
    Intent data = new Intent();
    data.putExtra("result", "papparapa. ");
    setResult(1, data );
    super.finish();
}

hence, in this case, the requestcode is 0 and the resultcode is 1,

back to the FirstFragment, I set the onActivityResult method.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0) {
        if (resultCode == 1) {
            if (data.hasExtra("result")) {
                finallist.add(data.getExtras().getString("result"));
                finallist.add("\n");
            }
        }
    }

and so far it is working!

Or at least, it is working in simple project with only this code, but, in my case, FirstFragment is a Fragment and it is giving nuisances.

While in a normal project, the final data imported is ultimately "pappara pippidi pooppeedeepoo", in my project here I get an empty message.

I paid attention at getting correctly the string from the onActivityResult ("data.getExtras().getString("result")") but I am quite sure it's all the fault of the Fragment.

Any idea on what could I do to fix this nuisance?

Last but not least: I would need to be able to click on one item in the ListView inside the NavigationDrawer and start a SHAREintent to share all these strings obtained from FirstFragment. Do I need to bridge the data from FirstFragment.class to MainActivity.class? If so, how can I do that?

Many thanks in advance!




Aucun commentaire:

Enregistrer un commentaire