Is it possible to save the state of a button in one activity and then pass it on to a new activity?

流过昼夜 提交于 2020-01-07 08:22:13

问题


My question pretty much sums up what I want to do.

My idea is if lets say (button 1 from activity A is pressed) and activity B is loaded I want to pass in activity B the state of button 1 (pressed or not) and I want to do something with it. For example:

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

回答1:


In the first activity, you can use:

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

and in the 2nd activity inside oncreate, you will use:

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



回答2:


Yes, keep the state of the button in activity A synchronised in preference storage, and initialise button B with that state after loading the layout in onCreate().

This is better than using intents because it adheres to the principle of separation of concerns. The synchronisation is restricted to the onClick event of button A. It also allows you to keep the state synchronised the other way from B to A without overriding the onBackPressed event or other lifecycle events.




回答3:


You can pass it through Bundles when calling the second activity thru intent from the first activity. Pass the state of the button using Boolean variable, and store it in your second activity. Then use that variable in your if condition.

In activity 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);

In Activity B:

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

if(buttonState)
{
 //do something
}



回答4:


You can do this through intents:

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

In the called activity's onCreate():

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



回答5:


You can call onSaveInstanceState manually to get the state. This returns an parcleable which you can use.

Pass it around and then call to restore:

youtView.onRestoreInstanceState(state);

Have a look on:

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




回答6:


This is a job for Intent Extras !

When you are launching your first Activity, do this :

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);

Then in Activity B :

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

Then use that value as you intended :

if(buttonState)
{
     //do something
}



回答7:


In first activity

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

In second (receiver activity) onCreate method

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


来源:https://stackoverflow.com/questions/21824343/is-it-possible-to-save-the-state-of-a-button-in-one-activity-and-then-pass-it-on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!