0

I have an Activity that starts a Fragment. Inside that fragment i have an EditText. And when i get the text that the user types there, i want to get that from my Activity with the help of an interface. I'm using this guide

On my MainActivity i'm implementing the commentListener interface and i've managed to get the result inside the onCommentEntered method. But on doneListener, which is triggered when the user presses a button finishing the activity, i'm getting null.

Obviously onCommentEntered runs after doneListener.

Any suggestions on how to get the result on doneListener?

class MainActivity implements fragment.commentListener{  

static String comment;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.addtransaction);
    done=(TextView)findViewById(R.id.done_button);
}

  // called when user presses a button and MainActivity finishes.

public void doneListener(View v){
    // Here i get NULL

    System.out.println(comment);
    finish();
}

 @Override
    public void onCommentEntered(String data) {
             comment=data;
             // Here i get what the user typed
             System.out.println(comment);
    }
}

My fragment

public class thefragment extends Fragment {

commentListener cListener;
static TextView note;
EditText comment;

public interface commentListener{
    void onCommentEntered(String data);
}

public static thefragment newInstance(){
    return new thefragment ();
}
public thefragment (){
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    cListener=(commentListener) context;
}

@Override
public void onDetach() {
    super.onDetach();
    cListener=null;
}

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


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

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

    comment=(EditText)v.findViewById(R.id.comment_picker);
    comment.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            if (!hasFocus) {
                  cListener.onCommentEntered(comment.getText().toString());
            }
        }
    });
    return v;
}
}
tasgr86
  • 299
  • 2
  • 7
  • 17
  • What is the purpose of sending a View as a parameter for doneListener method? – Dalma Racz Feb 08 '16 at 09:09
  • I've declared doneListener on my xml layout as a listener for the button (android:onClick). – tasgr86 Feb 08 '16 at 09:20
  • It does't look like you were following the guide. It uses java Interface as listener. So if you want your Activity to be your listener - it should implement a defined Interface and your fragment to use this Interface. – gioravered Feb 08 '16 at 09:25
  • If you declared in your xml android:onClick="doneListener" then it should work fine....could you post the stack trace please? – Dalma Racz Feb 08 '16 at 09:25
  • And also why don't you have class MainActivity implements commentListener ? – Dalma Racz Feb 08 '16 at 09:26
  • I've implemented commentListener on MainActivity.I forgot to add it here.Sorry. – tasgr86 Feb 08 '16 at 09:30
  • What do you mean by "getting null" in doneListener. what exactly is null ? – gioravered Feb 08 '16 at 09:55
  • In my fragment i have an EditText.When user clicks and enters some text, i pass that text to MainActivity.From onCommentEntered (of MainActivity) i get that text and i pass it on variable 'comment'.I am able to print 'comment' inside onCommentEntered (i get what user typed), but on doneListener 'comment' is empty. – tasgr86 Feb 08 '16 at 10:01
  • I understand, but you print the View and not the text - is that what you intended to do ? – gioravered Feb 08 '16 at 10:04
  • I edited the code before uploading and made i mistake.I wanted to print the 'comment' variable, not the view.Sorry for the confusion.Essentially onCommentEntered runs after doneListener and thus i get null in the first case. – tasgr86 Feb 08 '16 at 10:40
  • Could you please copy your XML as well ? – gioravered Feb 08 '16 at 11:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102895/discussion-between-gioravered-and-tasgr86). – gioravered Feb 08 '16 at 12:25
  • I think the problem is caused because i use OnFocusChangeListener for my EditText.In that case onCommentEntered method is called when my MainActivity finishes and 'comment' variable is thus null. If i switch to OnClickListener for my EditText then onCommentEntered is triggered before MainActivity finishes and i get what user typed on 'comment' as i wanted. However using onClickListener for my EditText is not an option as it listens only on the second click.Like this http://stackoverflow.com/questions/8397609/onclicklistener-listens-only-on-the-second-time – tasgr86 Feb 08 '16 at 12:48

2 Answers2

0

Create a new Interface, as well as implement the same in Fragment. create a method in the interface and override the same in Fragments. While calling the fragment from Activity create an Interface type fragment and call the interface method.

eg:

 public class thefragment extends Fragment implement fragmentNofyInterface {
   ...
   @Override
   protected void notify(String txt){
     mTvTxt.setText(txt);
   }
   .....
}

Interface Format

public interface fragmentNofyInterface {
     protected void notify(String txt);
}

Activity format

class MainActivity implements fragment.commentListener{  
       .....
       private fragmentNofyInterface mFragmentNotifier;

       .........

       thefragment  mFragment = new  thefragment();  
       mFragmentNotifier = (fragmentNofyInterface ) mFragment;
       FragmentTransaction transaction = mFragmentMngr.beginTransaction().
                            add(R.id.rl_fragment_navigation_container,
                                    mFragment);
       transaction .commit();

      ......

      //Notify the fragment when you required

      mFragmentNotifier.notify("hello world"); 


   }
Nithinjith
  • 1,775
  • 18
  • 40
0

Switching to addTextChangedListener and TextWatcher for my EditText, like this here, seems to fix my problem.Now onCommentEntered method is called before doneListener and thus String commentgets whatever was typed in the EditText.Thanks everyone for helping.

Community
  • 1
  • 1
tasgr86
  • 299
  • 2
  • 7
  • 17