0

Hi in my Activity I have a fragmentA with a textview, when I click on the textview , this fragmentA is replaced with fragment which has a listview. Now when I click on the litsItem I have to goback to fragmentA and update the textview with the list item.

Implementation:

I created an interface in fragment,

 public interface OnListItemSelectedListener {
            public void onListItemSelected(String msg);
        }

and in onAttach() I have the below code

 @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
           try {
                mListener = (OnListItemSelectedListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString() + " must implement OnListItemSelectedListener");
            }
    }

In my listitem OnClickListener() I have this

mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    String message = parent.getItemAtPosition(position).toString();
    mListener.onListItemSelected(message);

                    }
                });

then in my activity i implemented the interface

@Override
    public void onListItemSelected(String msg) {
        // TODO Auto-generated method stub
        ISOFragment myFrag = (ISOFragment)
                    getSupportFragmentManager().findFragmentById(R.id.isomain);

            if (myFrag != null) {

                myFrag.incrementdata(msg);
            } else {

                ISOFragment newFrag = new ISOFragment();
               Bundle args = new Bundle();
               args.putString("selecteitem", msg);
               newFrag.setArguments(args);

                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content_frame, newFrag);
                transaction.addToBackStack(null);
                transaction.commit();
    }

    }

Now since the fragmentA is not available it goes to else part now how should i get the bundle arguments to fragmentA and update the textview.

Suppose I am going from A to B and coming back from B to A, if i get arguments and update the textview in Fragment A in onCreateView(), if when i run for the first time it is checking for the arguments which will be null.

hari86
  • 659
  • 2
  • 16
  • 28
  • Possible duplicate of [How to use setArguments() and getArguments() methods in Fragments?](http://stackoverflow.com/questions/5425568/how-to-use-setarguments-and-getarguments-methods-in-fragments) – vilpe89 Jan 12 '16 at 08:48
  • @vilpe89 Hi I am going from A to B and coming back from B to A, if i get arguments and update the textview in Fragment A in onCreateView(), and when i run for the first time it is checking for the arguments which will be null – hari86 Jan 12 '16 at 08:53
  • Show the code how you are trying to get the arguments – vilpe89 Jan 12 '16 at 09:33
  • @vilpe89 this is how i am getting the arguments. String strtext=getArguments().getString("selecteitem"); boreselect.setText(strtext); – hari86 Jan 12 '16 at 09:36

1 Answers1

0

I had the same problem and the best solution for me was to extend activity and store shared data there. My new activity had a get data function that returned an object of class MyData implements Parcelable (Serializeable would work for simple data). The data was stored in onSaveInstanceState in the activity and restored in the activivitys onCreate.

Leif
  • 26
  • 5