0

I'm new to Android studio and i'm developing one location based app. Here, I need to close the app at some instance,so I called the finish(); function and killed the process in onDestroy()

Problem: Everything works fine in android 4.4(Kitkat) but crashes in Lollipop (Crashes at the second time of opening the app after install)

public void onClick(){
    finish();
}

@Override                   //------------after the finish(); called---//
protected void onDestroy() {
    Process.killProcess(Process.myPid());
    super.onDestroy();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

@Override
protected void onPause() {
    super.onPause();
}    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    from = (EditText) findViewById(R.id.from);
    to = (EditText) findViewById(R.id.to);
    go = (Button) findViewById(R.id.go);
    resulted = (TextView) findViewById(R.id.result);
    time = (TextView) findViewById(R.id.time1);
    button = (Button) findViewById(R.id.button);


    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);

     audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);


    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    pb = (ProgressBar) findViewById(R.id.progressBar1);
    pb.setVisibility(View.INVISIBLE);
    locationManager =            (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    go.setOnClickListener(new View.OnClickListener() {
                              public void onClick(View v) {

                                  str_from1=from.getText().toString();
                                  str_to1=to.getText().toString();

                                             str_from1 = str_from1.replaceAll("[^\\w]+", "+");
                                              str_to1 = str_to1.replaceAll("[^\\w]+", "+");
                                              new JSONTask().execute("https://maps.googleapis.com/maps/api/distancematrix/json?origins=" + str_from1 + "&destinations=" + str_to1 + "&mode=driving&language=fr-FR&key=API KEY");
                              }
                          }
    );
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Surya
  • 47
  • 8
  • Post your crash logs, that should have information about the crashes... – Aditya Feb 23 '16 at 10:51
  • 3
    Why are you even using `Process.killProcess(Process.myPid());`. `finish()` itself closes the activity. – Rohit5k2 Feb 23 '16 at 10:53
  • I don't know why it is not working, but you can try to call 'System.exit(0)' instead which is a nice way to close it – David Peicho Feb 23 '16 at 10:56
  • shoe logs of crashes – Neeraj Sharma Feb 23 '16 at 10:57
  • @DavidPeicho you shouldn't make jokes when he is trying to learn. Surya as Rohit said, there should be no reason to kill your pid. What are you trying to achieve? – Nick Cardoso Feb 23 '16 at 10:59
  • In Lolipop, it is fine working for the very first time. But, If I tried to open the app after I pressed the home button or back button.. it crashes. – Surya Feb 23 '16 at 10:59
  • @Nick Cardoso .. Yes I removed that " kill pid".... Still it crashing at the second time. – Surya Feb 23 '16 at 11:01
  • Post what it is doing onCreate and onResume, seeing as thats obviously the point at which its dying – Nick Cardoso Feb 23 '16 at 11:02
  • @Aditya and Neeraj .. I'm not using a emulator since my computer can't hold that heavy thing. So, I can't get the crash logs! Is any other way to find the crash logs? – Surya Feb 23 '16 at 11:04
  • @Nick I've posted the onCreate() things... and I didn't used the onResume in my code. – Surya Feb 23 '16 at 11:12
  • More important now is the logcat. At first glance besides the potential for NPEs, I also dont think you can call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); after setContentView and may be doing some network activity in the UI there – Nick Cardoso Feb 23 '16 at 11:13
  • My computer dying on opening the emulator.. So only I can't get the logcat.. please anyone tell alternate way that works fine for you. – Surya Feb 23 '16 at 11:16

1 Answers1

0

Well, you shouldn't need to kill the process. Calling finish() should be enough; it will destroy the current activity through its natural lifecycle, and if it's the last activity standing, the application will effectively quit.

For more discussion on why you should not go on that direction: Is quitting an application frowned upon?

Regards!

Community
  • 1
  • 1