I have a button in Acitivty that is created dynamically. But if I switch to another Activity, and back, the button disappears. How to make it there, even if I switch to another Activity?

  • You need to save the state in onSaveInstanceState , and then onRestoreInstanceState it in onRestoreInstanceState . Save the flag that indicates whether to show the button or not. - Nikita Remnev
  • @NikitaRemnev Can you write in detail how to do this, please? - Alexander

1 answer 1

You need to save the state in onSaveInstanceState , and then in onRestoreInstanceState restore this state. Full example:

TestActivity.java

 public class TestActivity extends AppCompatActivity { private LinearLayout dynamicButtonContainer; private boolean isButtonShowing = false; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); dynamicButtonContainer = findViewById(R.id.container); findViewById(R.id.add).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addDynamicButton(); } }); findViewById(R.id.start).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(TestActivity.this, OtherActivity.class)); } }); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("is_button_showed", isButtonShowing); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (savedInstanceState.getBoolean("is_button_showed", false)) { addDynamicButton(); } } private void addDynamicButton() { if (!isButtonShowing) { Button addedButton = new Button(TestActivity.this); dynamicButtonContainer.addView(addedButton); isButtonShowing = true; } } } 

activity_test.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add button"/> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start button" /> <LinearLayout android:id="@+id/container" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </LinearLayout> 
  • As a flag, you can create a variable that needs to be set to true when the button is shown (also set true when the button is added via onRestoreInstanceState), or if your button is a class field, you can simply check it for null - Nikita Remnev
  • My button is dynamically created. How can I add a variable to it? - Alexander
  • I'm just a beginner :) - Alexander
  • Attach the code of your Activity, it will be easier to tell) - Nikita Remnev
  • Add to the Activity field: private boolean isButtonShowing = false; and set isButtonShowing = true, at the moment of adding the button. Following onSaveInstanceState outState.putBoolean ("is_button_showed", isButtonShowing) - Nikita Remnev