0

I have a showAddForm button like the imej below in Claims.java.

enter image description here

When the + button is pressed, it will show Alert Dialog Window with radio buttons. When the radio button is checked, it will goes to specific activity. In the activity, it has an editText and a save button. I want the value on the editText display on the showAddForm button when the save button in the activity is clicked. How can I do to achieve this?

Claims.java

 public class Claims extends Fragment {
    private TextView c;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View claims = inflater.inflate(R.layout.claims, container, false);


        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialogRadio();

            }
        };
        Button button1 = (Button) claims.findViewById(R.id.button10);
        Button button = (Button) claims.findViewById(R.id.button8);
        button1.setOnClickListener(listener);
         c=(TextView)claims.findViewById(R.id.textView49);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
                startActivity(intent);
            }
        });
        return claims;
    }

    public void AlertDialogRadio() {
        final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
                , "Medical", "Other"};

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
        alt_bld.setTitle("Select a Claims");
        alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                .OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                if (item == 0) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                    startActivity(intent);
                } else if (item == 1) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                    startActivity(intent);
                } else if (item == 2) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
                    startActivity(intent);
                } else if (item == 3) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
                    startActivity(intent);
                } else if (item == 4) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
                    startActivity(intent);
                }

            }
        });
        AlertDialog alert = alt_bld.create();
        alert.show();


    }
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                String result=data.getStringExtra("text");
                c.setText(result);


            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }//onActivityResult
}

Assume the user choose Project.

Project1.java

  public class Project1 extends AppCompatActivity {
    private static String text;
    private static EditText txt;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.project);
       txt= (EditText)findViewById(R.id.editText36);
        Button b=(Button)findViewById(R.id.button17);

        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent returnIntent = new Intent();
                text = txt.getText().toString();
                returnIntent.putExtra("text", text);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

    }
}

Now when I click save button in Project1.java, it return to the AlertDialogRadio which is not I want and the text still not displaying on the textView!!! ANYONE CAN HELP?????

2 Answers2

1

You can use startActivityForResult() and override onActivityResult() in your activity. You can use setResult() in the second activity to pass whatever you want to your caller activity.

Example here and here


edit:

If you want it to use from a Fragment, you have to write this in your Activity that is holding the Fragment:

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

This will pass the result to your fragment, so you can handle the result inside your fragment.

And as I already mentioned, you have to use startActivityForResult() instead of startActivity()

Community
  • 1
  • 1
keybee
  • 1,498
  • 20
  • 32
0

as @keybee said you should use startActivityForResult() to start the project.class from your dialog. In the Project1.Class when you click save use setResult() to send the value back to the claims.class(Dont do startActivity() again. just use finish() ).

and in your Claims.class use onActivityResult(int requestCode, int resultCode, Intent data) to recieve the value sent from the project1.class. you can do a setText() on showAddForm button in the onActivityResult(int requestCode, int resultCode, Intent data) function

shreyas
  • 2,166
  • 3
  • 18
  • 30
  • I want it return to Claims.java, not AlertDialogRadio ...how can I do it? –  Oct 26 '15 at 09:55