I have two activities: activity_main, and car_activity. In activity_main there is a button to go to car_activity. In car_activity there is also a button that moves us to activity_main and takes a variable with it. When we went to activity_main, then there is a check. If the value we took with us is one, then a button is dynamically added. We have another ativity_third. In it we can go from activity_main, but when we go from activity_third to activity_main, this dynamic button disappears. Question: how to make sure that this button does not disappear?
activity_main:

public class Main2Activity extends AppCompatActivity { public void onBtm1(View v) { Intent int56 = new Intent(this, third.class); startActivity(int56); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); Integer value = intent.getIntExtra("value", 0); if (value > 0) { switch (value){ case 1: carFunc(); break; } } } private void carFunc() { LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainmain); ImageButton imageView = new ImageButton(Main2Activity.this); imageView.setImageResource(R.drawable.car); imageView.setScaleType(ImageView.ScaleType.FIT_XY); LinearLayout.LayoutParams imageViewLayoutParams = new LinearLayout.LayoutParams(450, 150); imageView.setLayoutParams(imageViewLayoutParams); imageView.setBackgroundColor(Color.TRANSPARENT); imageViewLayoutParams.setMargins(15,20,0,0); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent int46 = new Intent(Main2Activity.this, car.class); startActivity(int46); } }); mainLayout.addView(imageView); } 

car_activity:

 public class car extends AppCompatActivity { public void newBtn(View v) { Intent intent = new Intent(car.this, Main2Activity.class); intent.putExtra("value", 1); startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_car); } 

}

    0