title: Add items to listview from other activity
body: <p>scenario:</p>
  
  <p>First mainactivity launches and from the menu option user launches second activity using intent and there he adds some text to edittext and get that edittext value using intent to the first activity and add that value to the listview.</p>
  
  <p>FirstActivity:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      ListView lv;
      EditText et;
      String AddedTask ;
      ArrayList&lt;Model&gt; modelList;
      CustomAdapter adapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
                Intent intent = getIntent();
                  if (intent.hasExtra("NewTask")) {
                  AddedTask = this.getIntent().getExtras().getString("NewTask");
                  lv = (ListView) findViewById(R.id.listViewData);
                  String name = AddedTask;
                  Model md = new Model(name);
                  modelList.add(md);
                  adapter = new CustomAdapter(getApplicationContext(), modelList);
                  lv.setAdapter(adapter);
                  adapter.notifyDataSetChanged();
            }
  
  
  
      }
  
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar actions click
          switch (item.getItemId()) {
          case R.id.action_settings:
              return true;
          case R.id.action_add_task:
              Intent i = new Intent(MainActivity.this, AddTask.class);
              startActivity(i);
              return true;
          default:
              return super.onOptionsItemSelected(item);
          }
      }
  
  }
  </code></pre>
  
  <p>Second Activity:</p>
  
  <pre><code>public class AddTask extends Activity {
  Button addtask;
        @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.add_task);
  
              // get action bar   
              ActionBar actionBar = getActionBar();
  
              // Enabling Up / Back navigation
              actionBar.setDisplayHomeAsUpEnabled(true);
  
              addtask = (Button) findViewById(R.id.btnaddlist);
              findViewById(R.id.btnaddlist).setOnClickListener(
                        new View.OnClickListener() {
                            public void onClick(View arg0) {
                                EditText edit = (EditText) findViewById(R.id.tskname);
                                Intent i = new Intent(AddTask.this,
                                        MainActivity.class);
                                //Bundle bundle = new Bundle();
                                String TaskName = edit.getText().toString();
                                //bundle.putString("NewTask", TaskName);
                                i.putExtra("NewTask", TaskName);
                                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                //i.putExtras(bundle);
                                startActivity(i);
                            }
                        });
  
            } 
  
          }
  </code></pre>
  
  <p>Now my problem is I'm able to add the data to the listview but each time I come back to mainactivity the previous data which was added is lost and updating the old data with my new data.</p>
  
  <p>I have searched for many SO answers and most of them suggest to add <code>adapter.notifyDataSetChanged();</code> which I have already tried and nothing worked.</p>
  
  <p>I have done by checking the adapter is null or updating the data this way and getting null pointer exception:</p>
  
  <pre><code>if ( adapter== null )
  {
     adapter = new CustomAdapter(getApplicationContext(), modelList);
    lv.setAdapter(adapter);
  }
  </code></pre>
  
  <p>Can anyone say me how do I get this working ?</p>