Reputation: 88
my question is: If for example I have a RelativeLayout in Activity1, can I send that RelativeLayout to Activity2?
In my app I have 3 different RelativeLayout in Activity1, and if the user Clicked on one of those RelativeLayout I would to send it to my next activity. Any ideas on how to do that?
Upvotes: 0
Views: 127
Reputation: 808
I'll just give you an idea , you can send data from the first activity to the second using Intents , however you can create a layout dynamically using data received by the second Activity and adding all the parameters you need. I hope it will help you
Upvotes: 0
Reputation: 5312
From your comment it appears you need to load a different layout based on user's selection from previous screen. This problem can be tackled in several ways. One of them is to pass the user preference from Activity1
to Activity2
. To do this, in Activity1
:
int choice = 0; // assuming user made choice 0
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("choice", choice);
startActivity(choice);
Now, you can read the second value in Activity2
and accordingly load your layout (i.e. different view) as (within onCreate()
):
int choice = getIntent().getIntExtra("choice", 0); // default choice 0
switch(choice) {
case 0:
setContentView(R.layout.layout0);
break;
case 1:
setContentView(R.layout.layout1);
break;
default:
setContentView(R.layout.layout2);
}
Thus, based on user's choice you can choose a different layout and write you business logic accordingly thereafter.
Another option you have for your use case is that you define different activities for each of your layouts and then call startActivity()
to appropriate activity. This will separate out the business logic of handling the different layouts and may therefore be easy to maintain.
You can also go the fragment way but, ultimately it would also employ one of the above methods. Note that the method you choose highly depends on your use-case and therefore I cannot make that decision for you.
Upvotes: 1
Reputation: 4258
You can't do that since each Activity
has its own layout.
What you can do instead is send the data needed to setup your second Activity
's RelativeLayout
so that it looks like the one in your first Activity
via Intent
extras.
Upvotes: 1