Activity Lifecycle: Detect exit [Solved!]

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Activity Lifecycle: Detect exit [Solved!]

Postby rekire » Wed Sep 28, 2011 3:27 pm

Hallo,

I'm searching a way to detect the exit of my app. I know the Activity Lifecycle good!
How can I detect if my app is pushed into the background or is rotated or is closed by pressing back?

Please correct me but in every case the functions onPause, onStop and onDestroy are called, isn't it?
Last edited by rekire on Fri Oct 14, 2011 10:59 am, edited 2 times in total.
rekire
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Sep 28, 2011 3:16 pm
Location: Germany

Top

Re: Activity Lifecycle: Detect exit

Postby jonbonazza » Thu Sep 29, 2011 12:29 am

You can do this using onPause, onResume, onStop, onStart, and onDestroy.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Activity Lifecycle: Detect exit

Postby rekire » Thu Sep 29, 2011 8:27 am

Ehm sorry i wrote that yet! But i cannot detect what the reason for termination of the activitiy is!
rekire
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Sep 28, 2011 3:16 pm
Location: Germany

Re: Activity Lifecycle: Detect exit [Sloved!]

Postby rekire » Fri Oct 14, 2011 10:59 am

Here is my solution:
I have implemented an Application which is persistent due all Activities also if your last activity exits.

In that Application I have implemented the two methods informClientOnline() and informClientShutdown(). The first one sets a flag that the client is online. The second one starts a timer which checks delayed if the client has reconnected to the Application. Note: The Application is accessable by getApplicationContext().

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public abstract class AppObserver extends Application {
  2.         private static final String LOGTAG="AppObserver";
  3.         private static final int TIMEOUT=3000; //ms
  4.         private boolean clientOnline;
  5.  
  6.         // implement here your shutdown logic
  7.         public abstrace void shutdown();
  8.  
  9.         public void informClientOnline() {
  10.                 clientOnline=true;
  11.                 Log.v(LOGTAG, "Client is online!");
  12.         }
  13.  
  14.         public void informClientShutdown() {
  15.                 clientOnline=false;
  16.                 Log.v(LOGTAG, "Client is going offline. Waiting for restart...");
  17.                 Timer t=new Timer("shutdowntimer", false);
  18.                 t.schedule(new TimerTask() {
  19.                         @Override
  20.                         public void run() {
  21.                                 if(!clientOnline) {
  22.                                         Log.v(LOGTAG, "Client has not restartet! Shutting down...");
  23.                                         shutdown();
  24.                                         System.exit(0);
  25.                                 }
  26.                         }
  27.                 }, TIMEOUT);
  28.         }
  29. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
And the client calls:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.         setContentView(/* ... */);
  5.         // ...
  6.         ((AppObserver)getApplicationContext()).informClientOnline();
  7. }
  8.  
  9. @Override
  10. public void onDestroy() {
  11.         super.onDestroy();
  12.         ((AppObserver)getApplicationContext()).informClientShutdown();
  13. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
rekire
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Sep 28, 2011 3:16 pm
Location: Germany

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests