【问题标题】:Is it possible to save the state of a button in one activity and then pass it on to a new activity?是否可以将按钮的状态保存在一个活动中,然后将其传递给新活动?
【发布时间】:2014-02-17 08:43:38
【问题描述】:

我的问题几乎概括了我想做的事情。

我的想法是,如果让我们说(来自活动 A 的按钮 1 被按下)并且活动 B 被加载,我想在活动 B 中传递按钮 1 的状态(按下与否)并且我想用它做点什么。例如:

if(button1.isChecked())
{
     //do something
}

【问题讨论】:

  • 这是按钮或复选框
  • 嗯,我很确定我说的是按钮
  • 如何使用 isChecked() 和按钮

标签: android button


【解决方案1】:

是的,在首选项存储中保持 Activity A 中按钮的状态同步,并在 onCreate() 中加载布局后使用该状态初始化按钮 B。

这比使用意图更好,因为它遵循关注点分离的原则。同步仅限于按钮 A 的 onClick 事件。它还允许您保持状态从 B 到 A 的其他方式同步,而无需覆盖 onBackPressed 事件或其他生命周期事件。

【讨论】:

    【解决方案2】:

    您可以在通过 intent 从第一个活动调用第二个活动时通过 Bundles 传递它。使用布尔变量传递按钮的状态,并将其存储在您的第二个活动中。然后在您的 if 条件中使用该变量。

    在活动 A 中

      Intent saveButtonState =  new Intent(getApplicationContext(),B.class);
                        Bundle b = new Bundle();
                        b.putBoolean("Button_state", button1.isChecked()); //Save the button state in activity A
      saveButtonState.putExtras(b);
      saveButtonState.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity( saveButtonState);
    

    在活动 B 中

    Bundle b = getIntent().getExtras();
    Boolean buttonState = b.getBoolean("Button_state"); //Retreive button state in activity B
    
    if(buttonState)
    {
     //do something
    }
    

    【讨论】:

      【解决方案3】:

      您可以通过意图来做到这一点:

          Intent intent = new Intent(this, YOUR_ACTIVITY.class);
          intent.putExtra("BUTTON_STATE", button1.isChecked());
          startActivity(intent);
      

      在被调用activity的onCreate()中:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          boolean buttonState = getIntent().getBooleanExtra("BUTTON_STATE", false);
          ....
      }
      

      【讨论】:

        【解决方案4】:

        在第一个活动中,您可以使用:

        Intent intent = new Intent(this, Your 2nd Activity.class);
        intent.putExtra("buttonStatus", "you button status true or false");
         startActivity(intent);
        

        在 oncreate 内的第二个活动中,您将使用:

        boolean getButtonStatus = getIntent().getBooleanExtra("buttonStatus", false);
        

        【讨论】:

          【解决方案5】:

          您可以手动调用 onSaveInstanceState 来获取状态。这将返回一个您可以使用的 parcleable。

          传递它然后调用恢复:

          youtView.onRestoreInstanceState(state);
          

          看看:

          http://developer.android.com/reference/android/view/View.html#onSaveInstanceState()

          【讨论】:

            【解决方案6】:

            这是 Intent Extras 的工作!

            当您启动第一个 Activity 时,请执行以下操作:

            boolean buttonState; //The variable where you hold the button state in Activity A
            
            Intent intent = new Intent(this, ActivityB.class);
            intent.putExtra("button_state", buttonState);
            startActivity(intent);
            

            然后在活动 B 中:

            Intent intent = getIntent();
            boolean buttonState = intent.getBooleanExtra("button_state", false);
            

            然后按照您的意图使用该值:

            if(buttonState)
            {
                 //do something
            }
            

            【讨论】:

              【解决方案7】:

              在第一个活动中

              if(button1.isChecked())
              {
                  Intent i = new Intent(/* your intent */);
                  i.putExtra("KEY", value);
              } 
              

              在第二个(接收者活动)onCreate方法中

              if (savedInstanceState == null) {
                  extras = getIntent().getExtras();
                  if(extras == null) {
                      value= null;
              } else {
                   value= extras.getBoolean("KEY");
              }
              

              【讨论】:

                猜你喜欢
                • 2014-01-27
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-07-28
                • 1970-01-01
                • 1970-01-01
                • 2017-11-27
                • 1970-01-01
                相关资源
                最近更新 更多