【abinitio android】setContentView(int)をsetContentView(View)で置き換え、レイアウトを切り替えることで切り替え前の状態を保持できます
1.javaファイル
2.p21.xml
3.p22.xml
この方法では、2つのレイアウトを最初にロードしてキャッシュします.これにより、切り替え時に初期化を繰り返す必要がなくなります.
コードの簡略化は次のとおりです.
/**
*
*/
package com.itest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author Robin
*/
public class ITestP2Activity extends Activity {
Button b1;
Button b2;
View v1;
View v2;
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
// View1
v1 = getLayoutInflater().inflate( R.layout.p21, null );
// View2
v2 = getLayoutInflater().inflate( R.layout.p22, null );
// v1
setContentView( v1 );
// v1
b1 = (Button) findViewById( R.id.button1 );
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
jumpToDevicelistB4();
}
} );
}
void jumpToDevicelistB4()
{
setContentView( v2 );
b2 = (Button) findViewById( R.id.button1 );
b2.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
jumpToMainB4();
}
} );
}
void jumpToMainB4()
{
setContentView( v1 );
//
}
}
2.p21.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1" android:text="p21"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/changeText"></Button>
</LinearLayout>
3.p22.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1" android:text="p22"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/changeText"></Button>
</LinearLayout>
この方法では、2つのレイアウトを最初にロードしてキャッシュします.これにより、切り替え時に初期化を繰り返す必要がなくなります.
コードの簡略化は次のとおりです.
/**
*
*/
package com.itest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @author Robin
*/
public class ITestP2Activity extends Activity {
Button b1;
Button b2;
View v1;
View v2;
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
// View1
v1 = getLayoutInflater().inflate( R.layout.p21, null );
// View2
v2 = getLayoutInflater().inflate( R.layout.p22, null );
// v1
b1 = (Button) v1.findViewById( R.id.button1 );
b1.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
jumpToDevicelistB2();
}
} );
b2 = (Button) v2.findViewById( R.id.button1 );
b2.setOnClickListener( new OnClickListener() {
@Override
public void onClick( View v )
{
jumpToMainB1();
}
} );
// v1
setContentView( v1 );
}
void jumpToDevicelistB2()
{
setContentView( v2 );
//
}
void jumpToMainB1()
{
setContentView( v1 );
//
}
}