Sarius
Sarius

Reputation: 145

Passing views between Activities

i've got a "simple" question: How do i pass a view from an Activity to another?

In my case:

I've got a MainActivity with setContentView(R.layout.activity_main); In this activity_main i've declared a Relativelayout, whose Backgroundcolor I wanna change from another Activity, that gets its setContentView from an .xml file, where you cant find this RelativeLayout. How do i do that?

Thanks for the quick answer!

MainActivity.java

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        if (savedInstanceState != null) {

            //Save the text of the editText
            bringer = savedInstanceState.getString("bringer");

            input.setText(bringer);

            //Save the Listview
            content = savedInstanceState.getStringArrayList("content");

            adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, content);

            layout = (ListView) findViewById(R.id.layout);


            layout.setAdapter(adapter);
        }
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putStringArrayList("content", content);
        outState.putStringArrayList("list", list);

        bringer = input.getText().toString();
        outState.putString("bringer", bringer);
    }

Second Activity:

public class pop extends Activity {

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Thats where it fails, because it couldnt get the parent-
       view.
                finish();
            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


            }
        });
    }

}

Upvotes: 2

Views: 1097

Answers (1)

Ben P.
Ben P.

Reputation: 54204

The short answer is that you can't pass a View between activities.

If you have a View in ActivityOne that you want to modify based on the user's actions in ActivityTwo, then you should have ActivityOne launch ActivityTwo using startActivityForResult().

ActivityTwo can then call the setResult() method, and ActivityOne can implement onActivityResult() to receive that info.

Upvotes: 3

Related Questions