0

I am creating an application.It has many activity and fragment, such as it has login system and then the other function into the application.but when app crushes it's keep into logged.but the parameter remain empty.that's why the application send null value to the API. But Now I want to stop the complete app when any kind of problem occurs(app crushes). Anyone can suggest me !!!!

opu opu
  • 55
  • 6

3 Answers3

0

you can kill the complete app and remove it from running app list throught pid with this lines :

 int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
amir
  • 2,443
  • 3
  • 22
  • 49
  • But I want to stop application when it's unfortunately stopped.and I have so many pages and activity. The app can crush from any activity.So, then where can i wrote this code!! – opu opu Dec 28 '15 at 08:52
0
Create a background service or Broadcast receiver and check with logged in parameter value ,if parameter value is null than call

Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();`enter code here`
        System.exit(0);
Root4770
  • 48
  • 8
0

Since this is possible for any crash at any time at any activity, one way is to catch all the exception in the defaultExceptionHandler and provide your bail-out mechanism there i.e. on any such exception, kill the app, reset the parameters, etc.

Link: how to implement uncaughtException android

http://developer.android.com/reference/java/lang/Thread.UncaughtExceptionHandler.html

A gist of the implementation

  • Set a default exception handler to handle any crash that occurs anywhere.

    //Application.java

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
    }
    
  • Write an exception handler to handle any crash.

    // ExceptionHandler.java

    public class ExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler { private final Activity ctx;

    public ExceptionHandler(Activity ctx) {
        this.ctx= ctx;
    }
    
    public void uncaughtException(Thread thread, Throwable exception) {
        // Send mail or upload exception details if required.
        // kill app.
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(10);
    }
    

    }

Community
  • 1
  • 1
thepace
  • 2,221
  • 1
  • 13
  • 21