Skip to content Skip to sidebar Skip to footer

When Im Pressing Back Button My List Items Are Getting Deleted

In this activity i was trying to store data dynamically but when im pressing back button in my emulator my list items are getting deleted..... This is my main activity... pub

Solution 1:

Try to declare all variables out of your oncreate method..

publicclassGetListextendsActivity {
     ListView lv;
        Button bt3;

        ArrayAdapter<String> a_adapter;
        ArrayList<String> x_listItems;
        ArrayList<String> y_listItems;

    publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);

        lv = (ListView) findViewById(R.id.listView1);
        bt3 = (Button) findViewById(R.id.nameok1);
        bt3.setOnClickListener(newView.OnClickListener() {

            @OverridepublicvoidonClick(View v) {
                // TODO Auto-generated method stubIntent in3=newIntent(GetList.this,Dynamic.class);
                startActivity(in3);
            }
        });

        x_listItems = newArrayList<String>();
        y_listItems = newArrayList<String>();

        Intentin = getIntent();

        String inp = in.getStringExtra("input");
        String inp1 = in.getStringExtra("input1");

        if (null != inp && inp.length() > 0) {
            x_listItems.add(inp);

            a_adapter.notifyDataSetChanged();
        }
        if (null != inp1 && inp1.length() > 0) {
            y_listItems.add(inp1);
            Log.v("num", ""+y_listItems);


        }

         a_adapter = newArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, x_listItems);

         lv.setAdapter(a_adapter);
    }


}

Solution 2:

if you press back button when your on your GetList Activity the value of your ArrayList will be delete because the Activity is Destroy if you want to retain the value of the ArrayList make them

ArrayList<String> x_listItems;
ArrayList<String> y_listItems;

to this

publicstatic ArrayList<String> x_listItems;
publicstatic ArrayList<String> y_listItems;

Solution 3:

I'm not sure with this:

Just remove this two lines from OnStart and paste at OnCreate. I just say this according to android lifecycle. Removing OnStart is even better

et2 = (EditText) findViewById(R.id.number);bt2 = (Button) findViewById(R.id.numberok);

and I found some mistake in your code:

if (et.getText().length() == 0) {
            Contextcontext= getApplicationContext();
            CharSequencetext="Enter Your Name";
            intduration= Toast.LENGTH_SHORT;

            Toasttoast= Toast.makeText(context, text, duration);
            toast.show();
        } else {
            if (et2.getText().length() == 0) { // you noticed this as etContextcontext= getApplicationContext();
                CharSequencetext="Enter Your Number";
                intduration= Toast.LENGTH_SHORT;

                Toasttoast= Toast.makeText(context, text, duration);
                toast.show();
            }

Post a Comment for "When Im Pressing Back Button My List Items Are Getting Deleted"