title: Saving dynamically created button android
body: <p>I am trying to save a button that I make dynamically on a separate activity, but I cant seem to get it to work.  Here is what I have tried in my CreateButton.java:</p>
  
  <pre><code>    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
  
  public static int generateViewId() {
      for (;;) {
          final int result = sNextGeneratedId.get();
          // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
          int newValue = result + 1;
          if (newValue &gt; 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
          if (sNextGeneratedId.compareAndSet(result, newValue)) {
              return result;
          }
      }
  }
  
  public void Submit (View view) {
          Intent myIntent = new Intent(CreateNewClass.this, MainActivity.class);
          EditText mEdit   = (EditText)findViewById(R.id.class_name);
          String name = mEdit.getText().toString();
  
          mEdit   = (EditText)findViewById(R.id.class_semester);
          String semester = mEdit.getText().toString();
  
          mEdit   = (EditText)findViewById(R.id.class_year);
          String year = mEdit.getText().toString();
  
          Button myButton = new Button(this);
  
          if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.JELLY_BEAN_MR1) {
              myButton.setId(generateViewId());
          }
          else {
              myButton.setId(Button.generateViewId());
          }
  
          myButton.setText(name + " " + semester + " " + year);
  
          setContentView(R.layout.activity_main);
          LinearLayout ll = (LinearLayout)findViewById(R.id.main_screen);
          LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
                  (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
          ll.addView(myButton, layoutParams);
  
          startActivity(myIntent);
      }
  </code></pre>
  
  <p>And here is my .xml for the activity_main:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/main_screen"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
      &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/select" /&gt;
  
      &lt;Button
          android:layout_height="wrap_content"
          android:clickable="true"
          android:autoLink="web"
          android:layout_width="match_parent"
          android:id="@+id/button_so"
          android:text="@string/Google"
          android:linksClickable="true"
          android:onClick="goToGoogle"/&gt;
  
  
      &lt;Button
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:text="@string/newClass"
          android:autoLink="web"
          android:clickable="true"
          android:id="@+id/button_su"
          android:onClick="goToCreate"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>It creates the button dynamically on the mainActivity and navigates to it, but I cannot get it to save the button, it immediately goes back to just the two original buttons.</p>
  
  <p>Any help is really appreciated, I am a newbie and am just trying to learn some of this stuff on the side.  Thank you!</p>