无法调用父片段中的onActivityResult

时间:2016-03-10 08:41:19

标签: fragment return-value parent onactivityresult child-fragment

我是一个新手,我面临着一个问题,即从子片段到父片段发送数据已经有一段时间了,无论我做什么,我仍然无法解决它。我寻求了很多话题,我仍然无法解决它。如果可以的话,能帮帮我吗?我附上了所有的代码,如果我做了什么错,你可以教我吗?提前致谢 !     在这里输入代码

#################主要活动
public class MainActivity extends ActionBarActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManagercom = getFragmentManager();
        FragmentTransaction fragmentTransactioncom = fragmentManagercom.beginTransaction();
        cookskillcontent fcom = new cookskillcontent();
        fragmentTransactioncom.add(android.R.id.content, fcom);
        fragmentTransactioncom.addToBackStack(null);
        fragmentTransactioncom.commit();

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 0 ){
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
     //   getFragmentManager().findFragmentById(R.id.skill_id).onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}


###################################################
#### parent fragment  ###
###################################################


public class cookskillcontent extends android.app.Fragment{
    Button mmeatandveg;
    TextView mstepcontent;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v;
            v = inflater.inflate(R.layout.cookskillcontent, container, false);
            mstepcontent = (TextView) v.findViewById(R.id.stepcontent);
         //   mstepcontent.setText("ALL SHOW");
        mmeatandveg = (Button) v.findViewById(R.id.meatandveg);


        mmeatandveg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManagercom = getFragmentManager();
                caipu f1 = new caipu();
//getFragmentManager().findFragmentById(R.id.skill_id)
                f1.setTargetFragment(cookskillcontent.this
                        ,0);
                FragmentTransaction fragmentTransactioncom = fragmentManagercom.beginTransaction();
                fragmentTransactioncom.replace(android.R.id.content, f1);
//android.R.id.content
                fragmentTransactioncom.addToBackStack(null);
                fragmentTransactioncom.commit();
                // mActivity.TTT = "My fragment 1";

                //    SM.SendData("FRAGMENT 1");
                // mActivity.txt.setText("FRAGMENT one");
            }
        });

            return v;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       // mstepcontent.setText("ALLL");
      //  super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0 && resultCode == Activity.RESULT_OK) {
            if(data != null) {
                String value = data.getStringExtra("FRAGMXENT_KEY");
                if(value != null){
                    mstepcontent.setText("ALLL");
                    //  Log.v(TAG, "Data passed from Child fragment = " + value);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    /*
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // String value = data.getStringExtra("FRAGMENT_KEY");
        mstepcontent.setText("ALL SHOW 2");


        if(requestCode == 0 && resultCode == Activity.RESULT_OK) {
            if(data != null) {
                String value = data.getStringExtra("FRAGMXENT_KEY");
                if(value != null){
                    mstepcontent.setText("ALLL");
                    //  Log.v(TAG, "Data passed from Child fragment = " + value);
                }
            }
        }
    } */

}

#############################################################
### child fragment
#################################################################

public class caipu extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

    Button mcaipu1,mback;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v;
        v = inflater.inflate(R.layout.caipu, container, false);
        //   SM.SendData("FRAGMENT ttkx");
        mcaipu1 = (Button) v.findViewById(R.id.meatandveg1);
        mcaipu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });

        mback = (Button) v.findViewById(R.id.back);
        mback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("FRAGMENT_KEY", "Ok");
                getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
                getActivity().getFragmentManager().popBackStack();

            }
        });


        return v;
    }



}


###################### ########################################
#    here are layout xml file 
###############################################################
##  activity_main.xml  ####

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">



</LinearLayout>

#################################### 
#    caipu.xml
#
#####################################


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <Button
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:text="back"
        android:layout_gravity="center"
        android:textSize="12dp"
        android:id="@+id/back"
        />
    <Button
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:text="meat and veg1"
        android:layout_gravity="center"
        android:textSize="12dp"
        android:id="@+id/meatandveg1"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:text="Enter content"
        android:gravity="left|top"
        android:background="@android:color/darker_gray"
        android:id="@+id/vegvolume"/>


</LinearLayout>


################################################
##   cookskillcontent.xml
################################################

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/skill_id"
    android:orientation="vertical"
    >

    <TextView android:text="Create a Recipes for the World"
        android:background="@android:color/darker_gray"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center_horizontal"
        android:id="@+id/textView" />
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="40dp">
        <TextView
            android:layout_width="160dp"
            android:layout_height="40dp"
            android:text="SELECT STEP :"
            android:background="@android:color/darker_gray"
            android:id="@+id/steptext"
            />

        <Spinner
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:id="@+id/spinner"
            />
        <TextView
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:text="Current Step"
            android:background="@android:color/darker_gray"
            android:id="@+id/currentstep"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_height="90dp">
        <Button
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Meat And Veg"
            android:textSize="12dp"
            android:drawableTop="@drawable/vegicon"
            android:id="@+id/meatandveg"
            />

        <Button
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:text="Ingredient"
            android:layout_gravity="center"
            android:textSize="12dp"
            android:id="@+id/Button2"
            />

        <Button
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:text="Cooking Action"
            android:layout_gravity="center"
            android:textSize="12dp"
            android:id="@+id/Button3"
            />

        <Button
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_gravity="center"
            android:text="This Step Time"
            android:textSize="12dp"
            android:id="@+id/ettime"
            />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="280dp">
        <TextView
            android:layout_width="match_parent"
            android:background="@android:color/holo_blue_dark"
            android:layout_height="30dp"
            android:gravity="left|center"
            android:text="Every step  content"

            />
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="80dp">
            <Button
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_gravity="center"
                android:text="BACKDEL"
                android:textSize="12dp"
                android:id="@+id/backdel"
                />


            <TextView
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:gravity="left|top"
                android:background="@android:color/darker_gray"
                android:text="Display step content"
                android:id="@+id/stepcontent"
                />


        </LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="@android:color/holo_blue_dark"
            android:gravity="left|top"
            android:text="Write Cooking Notes "

            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:text="Cooking Notes content"
            android:gravity="left|top"
            android:background="@android:color/darker_gray"
            android:id="@+id/cookingnotes"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="50dp">
        <Button
            android:layout_width="140dp"
            android:layout_height="50dp"
            android:text="Save Recipes"
            android:id="@+id/saverecipes"
            />
        <EditText
            android:layout_width="260dp"
            android:layout_height="50dp"
            android:text="Enter File name"
            android:id="@+id/savefilename"
            />

    </LinearLayout>

</LinearLayout>

#########################################################

0 个答案:

没有答案