user2581076
user2581076

Reputation:

How to change toggle button state from another activity

Here is my question,my app flow is screen1,screen2,screen3.and their content views are like this:

screen1.java-->screen1.xml
 screen2.java-->screen3.xml
 screen3.java-->screen3.xml

here in screen1 if user clicks on toggle button to is going to screen2 then screen3 in screen3 payment success then only screen1 toggle button should change,how to achieve this,didn't get any idea,plz help me,Thanks.

Upvotes: 2

Views: 2625

Answers (5)

ZeeShaN AbbAs
ZeeShaN AbbAs

Reputation: 1365

You can pass button state using Bundle between activities like following

Start activity 2

Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_NAME, VALUE);
startActivity(intent);

Get that value in activity 2 like

@Override
protected void onCreate(Bundle savedInstanceState) {
....
boolean value = getIntent().getExtras().getBoolean(EXTRA_VALUE);
}

same like above you can pass is to Activity 3.

Or

You can make a static variable in you Activity 1 and then access that from Activity 3.

Upvotes: 0

MikeIsrael
MikeIsrael

Reputation: 2889

You could also pass the toggle state via intents and onActivityResult().

Here is a video tutorial on intents

Here is some a tutorial on onActivityResult

Upvotes: 0

keshav
keshav

Reputation: 3255

You can make that toggle button static and then you can change its state from any activity.

 static ToggleButton  toggleButton = (ToggleButton)findViewById(R.id.toggle_btn);

but you need to be careful.

Upvotes: 0

Rahul Gupta
Rahul Gupta

Reputation: 5295

Use a static global array list and save the pressed states at their positions in that list. In the next activities use that array list to set the toggle status of your buttons

This is similarly like saving the checked states of a checkbox in a listview

Upvotes: 1

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

You can save the state of toggle button in Shared preferences. Check this : Shared Preferences Android and Example

Hope this helps.

Upvotes: 1

Related Questions