-2

I have developed an application which uses some external open source projects. For now I have made sure that there is no place where the application would force close. However, I don't want to take chances considering the external opensource apps. I want to make sure that if in case my application force closes, I restart the application from the start.

I searched through the net for a solution and the best I could find was here. However I really couldn't understand how to implement it...

I have seen many applications which open the first screen on force close, so can you give me some some assistance?

Community
  • 1
  • 1
Vivekanand
  • 755
  • 1
  • 8
  • 29

2 Answers2

2

The Link you posted is the way to do it. Best will be if you add an Android Application if not already present. Register an UncaughtExceptionHandler Inside Your Applications onCreate Method just like this:

//Inside your ApplicationClass
public void onCreate(){
    Log.v("MyApplication", "onCreate triggered")
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(){
         public void uncaughtException(Thread thread, Throwable ex){
             Log.v("MyApplication", "onUncaughtException triggered. Error:")
             ex.printStackTrace()
             //restart your app here like this
             Intent i = new Intent(this, TheClassYouWannaStart.class);
             getApplicationContext().startActivity(i);
         }
    });
}
Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • have you verified that Apllications.onCreate is run? Put a Log inside like in my updated answer, and check it inside DDMS – Rafael T Apr 02 '12 at 16:07
  • @vivek Are you actually placing this into an application class or an Activity? This won't work in an Activity. – ahodder Apr 02 '12 at 16:07
  • i have this main page and 6 icons in a grid view... where each icon leads to a different activity.... so i want to check at the main page for exception – Vivekanand Apr 02 '12 at 16:11
  • 1
    @vivek as AedonEtlira stated: THIS will not work. I made clear that you have to put this inside an Android APPLICATION. Check initial Answer for a link how to invent an Application... Its easy – Rafael T Apr 02 '12 at 16:13
0

I am assuming that you mean service. If you had an application that always made itself present, I would hate you. But for a service, just start it with the START_STICKY flag.

Example

public MyAmazingService extends Service {
    @Override public int onStartCommand(Intent intent, int arg1, int arg2) {
        // Doing some amazing stuff
        return START_STICKY;
    }
}   
ahodder
  • 11,353
  • 14
  • 71
  • 114
  • I guess he just wants to keep his Activity alive when it was closed by an Exception... You're right, if there is a bug inside his first Activity, which could cause an "infinite loop" – Rafael T Apr 02 '12 at 15:17
  • @RafaelT That would be a better assumption than mine. Good call sir. – ahodder Apr 02 '12 at 15:19
  • no its not services i'm talkin bout.... In terms of unexpected force close i want to close the application and restart it. – Vivekanand Apr 02 '12 at 15:55
  • @vivek Then go with RafaelT's answer. However, I **STRONGLY** disagree with that design choice. You are making a decision that should be left up to the user [to reopen the app]. – ahodder Apr 02 '12 at 16:07