how to program on application(not activity) exit ?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

how to program on application(not activity) exit ?

Postby hnmapara » Wed Jul 22, 2009 8:30 pm

Hi all,

I have requirement that needs to save data to a file. During multiple Activitiy screens, I add different data to a list
and finally when user is not using the app (means if user exits app by pressing "home" button or "call" button), just before exiting that app, i want to save that data of the list to a file.

coding before exiting activity screen is simple - by calling onDestory() method, but here I want not one activity but the application exit.

So my question is How do I write a code that will be executed just before application exits ?
I tried created one class extends Application, and then I override onTerminate() method, but it's not get called when I exit ( pressing home button on device)

Plz help
Thanks
Harshit
hnmapara
Freshman
Freshman
 
Posts: 9
Joined: Wed Jul 22, 2009 8:19 pm

Top

Postby arams » Thu Jul 23, 2009 6:01 am

over write the exit and call buttons

KeyEvent.KEYCODE_HOME
KeyEvent.KEYCODE_CALL
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby virtual » Fri Jul 24, 2009 1:33 pm

Yes just handle call and home keys. And when the last activity is destroyed just call

getApplication().onTerminate();

for e.g

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. void showConfirmationAlert(){
  5.  
  6.                 new AlertDialog.Builder(this)
  7.  
  8.         .setTitle("Quit")
  9.  
  10.         .setIcon(R.drawable.anyicon)
  11.  
  12.         .setMessage("Exit Application?")
  13.  
  14.         .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  15.  
  16.                 public void onClick(DialogInterface dialog, int whichButton) {
  17.  
  18.                         YourActivity.this.finish();
  19.  
  20.                         getApplication().onTerminate();
  21.  
  22.                 }
  23.  
  24.         })
  25.  
  26.         .setNegativeButton("No", new DialogInterface.OnClickListener() {
  27.  
  28.                 public void onClick(DialogInterface dialog, int whichButton) {
  29.  
  30.                        
  31.  
  32.                 }
  33.  
  34.         }).show();
  35.  
  36.         }
  37.  
  38.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

In Application onTerminate just do what you want. :)
Just force the application to kill itself.
Just an idea.
Regards,
Virtual
virtual
Developer
Developer
 
Posts: 26
Joined: Tue Mar 24, 2009 11:20 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests