M
M
Max Serge2020-04-26 18:36:25
Java
Max Serge, 2020-04-26 18:36:25

2 Activity Android studio How to use second intern?

The android application has 2 activities, each has a button and a text field, if you enter something in the text field, then by clicking on the button, we go to the second layer, and the text that we entered earlier will be displayed there, in the opposite direction as well.
The problem is in intern, the pair 1-2 works fine, but 2-1 does not, although the code is copied and only the values ​​\u200b\u200bof where the text comes from and where to paste it are changed.
MainActivity:

EditText txt;


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

        txt = (EditText)findViewById(R.id.txt_data);

        Button button = (Button) findViewById(R.id.buttonOpendialog);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              openActivity2();
            }
        });

    }

    protected void OnActivityResult(int requestCode, int resultCode, @Nullable Intent data){

        if (requestCode == 555)
        {
            if(data !=null)
            {

                String s = data.getStringExtra("qwe");
                Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
                EditText txt_data = findViewById(R.id.txt_data);
                txt_data.setText(s);

                txt.setText(s);

            }

        }
        super.onActivityResult(requestCode, resultCode,data);
    }

    public void openActivity2() {
        String s = txt.getText().toString();

        Intent intent = new Intent(this, Main2Activity.class);
        intent.putExtra("abc",s);
        startActivityForResult(intent,555);
    }


2MainActivity:
EditText txt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    txt = findViewById(R.id.txt_data_alt);
    Intent intent = getIntent();
    String s = intent.getStringExtra("abc");
    txt.setText(s);
    }

    public void on_ok_click(View v)
    {
        Intent i = new Intent();
        String s = txt.getText().toString();
        i.putExtra("qwe",s);
        setResult(RESULT_OK,i);
        finish();
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question