Android: Is Using Setcontentview Multiple Times Bad While Changing Layouts

Android: is using setContentView multiple times bad while changing layouts?

Let's take a look at the Android Documents:

Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.

So, setContentView will overwrite the layout, and replace it with a new one. Usually, you only want to do this once in onCreate. Theoretically, you could do it more, but it involves re-drawing the entire layout, and this could take some time. There are a few alternatives, depending on exactly what you want:

  1. ViewAnimator: This is useful for showing a quick animation, if you want to change the view multiple times in quick succession.
  2. Fragments- Instead of re-drawing the entire view, you can switch out fragments. Each fragment is a kind of mini activity, and overall this will contain the code much better.
  3. Pass Intent Arguments- Pass information to an activity to help it set up. The first activity passes information to a common second activity, which knows how to set itself up based off of the information it receives from the first activity.

As for your specific application, here's what I would do:

  1. Each band follows a specific layout. There is only 1, or maybe a few, possible layouts.
  2. When the Band activity starts, the appropriate layout is chosen, and populated, knowing what's in there.

The Android SDK shows how to pass data from one activity to another. Just pass the data that the second activity needs from the first, using something like this:

Intent intent=new Intent(...);
intent.putExtra("Album","Some Album")
startActivity(intent);

The second activity will do this:

Intent intent=getIntent();
String albumName=intent.getExtraString("Album");
//Does something with albumName, maybe get a TextView and .setText()

Calling setContentView() multiple times

Per Austyn's comment, I did manage to locate some guidance on how to use ViewFlipper to accomplish this in another post (see the checkmarked top answer here.)

If you don't want to use ViewFlipper, I found a nice example of how to switch between layouts in the same view here:

XML:

<FrameLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="@drawable/icon"
android:scaleType="fitCenter"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
<TextView
android:text="Learn-Android.com"
android:textSize="24sp"
android:textColor="#000000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>

Code:

private void SwitchLayout2() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 2 and Disable Layout 1
Layout1 .setVisibility(View.GONE);
Layout2.setVisibility(View.VISIBLE);
}

private void SwitchLayout1() {
RelativeLayout Layout1 = (RelativeLayout)findViewById(R.id.layout1);
RelativeLayout Layout2 = (RelativeLayout)findViewById(R.id.layout2);

// Enable Layout 1 & Disable Layout2
Layout1.setVisibility(View.VISIBLE);
Layout2.setVisibility(View.GONE);
}

How to keep information when I change layout android studio

intead of using overriden onRestoreInstanceState use same restoring code in onCreate(Bundle savedInstanceState) using its savedInstanceState argument. best approach would be to implement shared method for restoring data and call it in both onCreate and onRestoreInstanceState (remember to check if Bundle is null, as at very first start of Activity it is in onCreate)

Android apply settings after setContentView

After you set the XML again via (setcontentview) in configurationChange, initialize the findviewbyid button. That should work.

Android error on change layout in same activity

Thanks too much for your effort Barak , Alex Lockwood and the others too i recode my xml layout upon your advices nothing changed but i could seen an error cant be realize before due to my unnecassary LinearLayout block .Whatever the error is :there is a statement in my layout

android:textStyle="@style/boldText" 

in this statement the value can be only bold or normal etc .this statement should be like

style="@style/choiceStyle"

thanks for your help .I decided not to work after 00:00

Cannot change the layout using setContentView (or loading a Fragment) after screen rotation?

1) When the Activity is recreated, you are losing scope of the original activity (this) in the implied this.SetContentView(whatEverView);.

2) In your Fragment's OnCreateView, you are internally using the Context of the ViewGroup which is a MainActivity instance, but this will be changing every time the Activity is recreated due to a configuration change. OnAttach and OnDetach should be used to assign the current Context/Activity and to clear/null it.

3) Instead of using a static var for your CustomFragment, the FragmentManager.FindFragmentByTag should used and the fragment recreated as needed as Fragments can be reclaimed by the OS even if it is set to be retained, you will be chasing phantom crashes in the wild...

This is a quick fix example, the internals of CustomFragment still need to reset/clear the old event handlers when the fragment is detached from the Activity and correct the Button's use of the old/new context.

public class MainActivity : AppCompatActivity
{
const string customFragmentTag = "custom_fragment";
CustomFragment frag;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);

frag = FragmentManager.FindFragmentByTag(customFragmentTag) as CustomFragment;
if (frag == null)
{
frag = new CustomFragment(Resource.Layout.Frag);
FragmentManager.BeginTransaction().Add(Resource.Id.container, frag, customFragmentTag).Commit();
}
frag.ButtonClick += (s, e) => SetContentView(Resource.Layout.Frag);
}
}

If your Fragment needs access to the current Context/Activity, you should implemented the following overrides (currentActivity is a class level Activity var):

public override void OnAttach(Activity activity)
{
base.OnAttach(activity);
currentActivity = activity;
}

public override void OnAttach(Android.Content.Context context)
{
base.OnAttach(context);
currentActivity = context as Activity;
}

public override void OnDetach()
{
base.OnDetach();
currentActivity = null;
}


Related Topics



Leave a reply



Submit