1

I have an application which have 2 button, save and exit. save button to insert sqlite and reset all EditText and listview,Exit button to close my android application.here is my save button code :

 btnsaveto = (Button) findViewById(R.id.btnsaveto);
      btnsaveto.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
        //do insert into sqlite
        Globals.mylist.clear();//my listview
        Globals.outletname=null;//my Editext
        Globals.outletid=0;//my Editext
        Globals.notes=null;//my Editext
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
     });

and here is my exit button's code:

btnexit = (Button) findViewById(R.id.btnexit);
      btnexit.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            finish();
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            }
        } );

Here is my oncreate activity :

  super.onCreate(savedInstanceState);
      setContentView(R.layout.entry_to);
      txtoutlet = (TextView) findViewById(R.id.txtoutlet);
      dataSource = new DBDataSource(this);
      dataSource.open();
      Bundle bun = EntryTO.this.getIntent().getExtras();
          try{
      if (Globals.mylist == null){Globals.mylist = new ArrayList<HashMap<String, String>>();}
            id = bun.getLong("id");
            Id=String.valueOf(id);
            description = bun.getString("description");
            qty = bun.getString("qty");
            if(bun.getString("outletname")!=null){
            Globals.outletid = bun.getLong("outletid");
            Globals.outletname = bun.getString("outletname");  
            }
          map1 = new HashMap<String, String>();
          if(Globals.mylist.size() == 0){
              if(Id!=null && description!=null && Globals.outletname!=null){
              map1.put("one", Id);
              map1.put("two", description);
              map1.put("three", qty);
              Globals.mylist.add(map1);
              txtoutlet.setText(Globals.outletname);
              }else{
              txtoutlet.setText(Globals.outletname);  
              }
          }else{
          int j=Globals.mylist.size();
          if(Id!=null && description!=null && Globals.outletname!=null){
          map1.put("one", Id);
          map1.put("two", description);
          map1.put("three", qty);
          Globals.mylist.add(j,map1);
          txtoutlet.setText(Globals.outletname);
          }else{
              txtoutlet.setText(Globals.outletname);
              } 
          }
            try {
                adapter = new CustomAdapter(EntryTO.this, Globals.mylist, R.layout.item_to,
                          new String[] {"two", "three"},
                          new int[] {R.id.edtbrandto,R.id.edtqtyto });
                setListAdapter(adapter);

            }catch (Exception e) {}
      }catch(NullPointerException e){}
      btnsaveto = (Button) findViewById(R.id.btnsaveto);
      btnsaveto.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
        try{
        String orderid=orderid();
        String orderdate=orderdate();
        String notes = Globals.notes;
        String image = photoToBase64String() ;
        long outletid = Globals.outletid;
        order = dataSource.createorder(orderid, orderdate, notes, image , outletid);
        for(int i=0;i<=Globals.mylist.size()-1;i++){
            map1=Globals.mylist.get(i);
            String Id = map1.get("one");
            String qty = map1.get("three");
        orderdtl = dataSource.createorderdtl(orderid, Id, qty);
        Toast.makeText(EntryTO.this, "Mengisi Data Ke-"+i+", tunggu sebentar..\n" ,Toast.LENGTH_LONG).show();
        }
        Globals.mylist.clear();
        Globals.outletname=null;
        Globals.outletid=0;
        Globals.mPath=null;
        Globals.notes=null;
        Intent intent = getIntent();
        finish();
        startActivity(intent);
        }catch(NullPointerException e){
            }
        }
     });

The problem was when I re-open my application, EditText and ListView still fill. Is there something wrong when i reset intent, or I haven't close the app correctly.

NB: I have 3 Activity, and I Declared all of my variable on Globals.java to keep my values on some activity when I get other values on another activity.

user3264399
  • 300
  • 1
  • 12
WardaLyn
  • 154
  • 1
  • 1
  • 10
  • Try adding the flag clear task also to new task flag – Rat-a-tat-a-tat Ratatouille Feb 03 '14 at 04:31
  • are you filling your `EditText` and `ListView` in the `onCreate()` method? – SMR Feb 03 '14 at 04:32
  • @SMR yes you alright.. – WardaLyn Feb 03 '14 at 04:34
  • whatever you do in the `onCreate()` method then it is going to be executed every time you launch your app. if you dont want the data to be loaded every time then you need to it anywhere else. can you provide the full activity code? – SMR Feb 03 '14 at 04:37
  • @WardaLyn if you are filling your `EditText` and `ListView1 in `onCreate` from SQLite then yes whenever you open your app your data will be filled in them autometically. – Atif Farrukh Feb 03 '14 at 04:41
  • @SMR i have add my oncreate activity. – WardaLyn Feb 03 '14 at 04:46
  • @AtifFarrukh what should i do? i try to avoid them by reset, and clear all values on globals when i finished insert all values into sqlite.any advice? – WardaLyn Feb 03 '14 at 04:50
  • when do you want the data to be shown or hidden to the user? – SMR Feb 03 '14 at 04:55
  • if you want to poplate your `EditText` and `ListView` "whenever you want" just add a button and in `onClickListener` query your database. In this case when you press `Button` your activity will load data from database. make sure to set text of your `EditText` and `ListView` after query ie in the `onClickListner`. – Atif Farrukh Feb 03 '14 at 04:55
  • @SMR Only when I open my application, all of my editText and listview on my parent activity cleared. I need to fill them up with new values.could you help me – WardaLyn Feb 03 '14 at 05:04
  • All right I can help you only if I understand what are you trying to do in your app. If I clearly understand what you are trying to achieve then i will be able to help you. – SMR Feb 03 '14 at 05:29
  • http://stackoverflow.com/questions/4342761/how-do-you-use-intent-flag-activity-clear-top-to-clear-the-activity-stack – Shafeek CP Feb 03 '14 at 05:31
  • if filling my edittext and listview in onCreate makes all of them filled in, where should i do?@SMR – WardaLyn Feb 03 '14 at 05:39

5 Answers5

1

you can not close your application. android does not allow this. you can invoke home intent sot that your application will get back in background.

Waqar Ahmed
  • 4,682
  • 1
  • 19
  • 38
1

If your application contains only one activity then you should consider using finish() method or super.onBackPressed().

If your application has more than one activity and lets say you have login page or splash activity (Whichever is first activity to launch application) lets call it A, then you call acitivty A with FLAG_CLEAR_TOP and pass some value through bundle and in A check if you want to exit app or not, if yes then finish that activity.

Pankaj
  • 1,190
  • 1
  • 8
  • 21
0

Try this.....

finish();
System.exit(0);
Dev 9
  • 263
  • 2
  • 5
0

You can maintain an activity stack that:

  1. pushes new activity when it is created
  2. pops activity when it is destroyed

When you need to close application, you call finish() on every activity in the stack.

Krypton
  • 3,278
  • 5
  • 30
  • 50
0

You just use the below code to exit from recent.

activity android:name=".AppName" android:excludeFromRecents="true"