使用相同的Activity实例

时间:2015-10-28 15:17:09

标签: android android-activity

我正在制作一个游戏,每当用户失败时我想使用相同的Activity,并且每次显示FailedActivity时我都不想创建所有的视图。
例如,我有Level1Activity:

public class Level1Activity extends Activity{

   private static final int REQUEST_FAIL_ACTIVITY = 10;
   private static final int RESULT_RETRY_LEVEL= 11;
   public void onCreate(Bundle savedIntanceState){
       ...
   }

   public void showFailedActivity(){
       Intent intent = new Intent(this, FailedActivity);
       intent.putExtra("text1", "You failed! You made only 50 points");
       intent.putExtra("text2", "Try again later");
       startActivityForResult(intent, REQUEST_FAIL_ACTIVITY );
   }

   protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == REQUEST_FAIL_ACTIVITY ){
            if(resultCode == RESULT_BACK){
                finish();
            }else if(resultCode == RESULT_RETRY_LEVEL){
                retryLevel();
            }else{
                finish();
            }
        }
    }
}

类FailedActivity

    private RelativeLayout bgLayout;
    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private TextView text5;
    private Button buttonRetryLevel;
    private Button buttonBack;

    public class FailedActivity extends Activity{
        public void onCreate(Bundle saved){
           super.onCreate(saved)

           setContentView(R.layout.activity_failed);
           bgLayout = (RelativeLayout)findViewById(R.id.bgLayout);
           text1 = (TextView)findViewById(R.id.textView1);
           ...
           buttonBack = (Button)findViewById(R.id.btnBack);

           Bundle bundle = getIntent().getExtras();
           text1.setText(bundle.getString("text1");
           ...
           text5.setText(bundle.getString("text5);

        }

     public void buttonRetryClicked(View view){
        setResult(Level1Activity.RESULT_RETRY_LEVEL);
        finish();
     }

     ...
}

所以,我想只为所有视图调用一次findViewById,并重用Activity,因为我用于所有级别的FailedActivity,我只需要传递将显示的字符串,可能是背景颜色等。

我的问题是:我可以只创建FailedActivity,然后当我调用startActivityForResult只是为了传递Bundle和Views只创建一次。

2 个答案:

答案 0 :(得分:0)

您可以使用

  

singleInstance

作为启动模式并覆盖onNewIntent()函数以绕过重新创建活动UI组件。

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    // handle parameters here.
}

答案 1 :(得分:0)

是的,这是可能的,如另一个答案所述。如果您这样做,那么您将超出标准的Activity生命周期。通常,当Android应用程序启动一个新的Activity时,旧的Activity将被销毁,并创建一个新的新实例。更改设备方向(纵向 - >横向)时也会发生这种情况。 Android运行时执行此操作以提高内存效率,因为视图维度可能已更改。如果您想了解有关活动生命周期的更多信息,请查看this

您从Level1Activity投入意图的捆绑值对于其他级别是否相同?如果是这样,您可以将这些字符串直接硬编码到FailedActivity的xml布局文件中。如果您需要所有级别的活动都能够打开FailedActivity,我会考虑创建一个levelActivity超类。这将简化FailedActivity创建代码并阻止您不得不重复自己。像这样:

public abstract class LevelActivity extends Activity {
    public void showFailedActivity(){
        Intent intent = new Intent(this, FailedActivity);
        intent.putExtra("text1", "You failed! You made only 50 points");
        intent.putExtra("text2", "Try again later");
        startActivityForResult(intent, REQUEST_FAIL_ACTIVITY );
    }
}

public class Level1Activity extends LevelActivity {
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_FAIL_ACTIVITY ){
        if(resultCode == RESULT_BACK){
            finish();
        }else if(resultCode == RESULT_RETRY_LEVEL){
            super.retryLevel(); // calls LevelActivity.retryLevel()
        }else{
            finish();
        }
    }
}