Reputation: 13
I have first.class and second.class. How I can change ContentView (xml file) in first.class by click button, who is in second.class? (Maybe my question is to simple, but I can't find the answer.)
Upvotes: 0
Views: 1077
Reputation: 86948
You can use a simple flag. Read the flag in your first Activity from a Bundle or the disk (See Data Storage). In the first Activity's onCreate()
method use something like this:
// Read the flag, in this case from an Intent
int choice = 0;
Intent intent = getIntent();
if(intent != null)
choice = intent.getIntExtra(LAYOUT_PREFERENCE, 0);
// Load the appropriate layout
switch(choice) {
case 0:
setContentView(R.layout.one);
break;
case 1:
setContentView(R.layout.two);
break;
//etc
}
Set this flag in the second Activity, specifically inside the Button's OnClickListener. Again I used a simple Bundle which you can pass via setResult()
or even startActivity()
Upvotes: 1
Reputation: 2738
You have to store the setting for your first Activity (either layout1 or layout2) somewhere, where both Activities can access the value and your second Activity can write it also. You can use SharedPreferences, static variables, or pretty much anything mentioned in the Android Developer Guide.
If you are always starting the first activity from the second Activity, you can also add the layout as an extra to the calling intent.
Upvotes: 0