Is It Possible To Save The State Of A Button In One Activity And Then Pass It On To A New Activity?
Solution 1:
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.
Solution 2:
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
}
Solution 3:
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);
....
}
Solution 4:
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);
Solution 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()
Post a Comment for "Is It Possible To Save The State Of A Button In One Activity And Then Pass It On To A New Activity?"