Reputation: 154
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.
Upvotes: 1
Views: 377
Reputation: 1
You just use the below code to exit from recent.
activity android:name=".AppName" android:excludeFromRecents="true"
Upvotes: 0
Reputation: 1242
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.
Upvotes: 1
Reputation: 3325
You can maintain an activity stack that:
When you need to close application, you call finish() on every activity in the stack.
Upvotes: 0
Reputation: 5068
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.
Upvotes: 1