-1

I have a reset button in Activity A and it works fine since it can clear all the text and display null when the save button in Activity B is clicked. But this only works if there are nothing in the textView before pass to B.

It does not works in below cases.

In Activity A, type " Project 123', click next button go to B. Then I back to Activity A again and click the reset button to clear "Project 123". After done go to Activity B and click submit button. It shows "Project 123" instead of "null"...

Activity A

private TextView c;
String result; // 123
String name; // project

reset=(Button)claims.findViewById(R.id.button14); // reset button
Button button = (Button) claims.findViewById(R.id.button8); //next button

reset.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        c.setText("");
        d.setText("");
        e.setText("");
        f.setText("");
        g.setText("");
        h.setText("");
    }
});

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
        Intent intent = new Intent(getActivity().getApplicationContext(), B.class);
        if(c!=null){
            intent.putExtra("name", name);
            intent.putExtra("result", result);
        }
    });
    return A;
}

Activity B

Name=getIntent().getExtras().getString("name");
Result=getIntent().getExtras().getString("result");
save=(Button)findViewById(R.id.button8);
save.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        if((Name!=null)&&(Result!=null)){
            Toast.makeText(getApplicationContext(), Name+Result, Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(),"null", Toast.LENGTH_LONG).show();
        }
    }
});
braX
  • 11,506
  • 5
  • 20
  • 33
Tony
  • 2,515
  • 14
  • 38
  • 71

2 Answers2

0

Actually, the name is not null but with empty string "" Try to take replace the Name != null with !TextUtils.isEmpty() in B activity.

Weibo
  • 1,042
  • 8
  • 18
  • @sengTony looks you did not get the name from the text in click method, and also please do TextUtils.isEmpty for variable result. – Weibo Oct 30 '15 at 03:28
0

This is because you only clear the setText but not the string.

 reset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
               c.setText("");
                name="";
                result="";


            }
        });

You have to add name="" and result="" in your reset method. It should works.

Kindly refer clear a value from a string android

Community
  • 1
  • 1
Hoo
  • 1,806
  • 7
  • 33
  • 66