-9
Intent intent = new Intent(banana.this, super.class);
        intent.putExtra("username1", username1_who_logged);
        startActivity(intent);
        System.exit(1);

I'm having outOfMemory error so I solved it with the code above.

Is it good practice to do it? I still need to pass some data, which is comes from "extra" with intent.

I pretty much erase or dump the entire system stuff(don't know exactly what I do with system.exit(1) though!

Is this a good practice?

ERJAN
  • 23,696
  • 23
  • 72
  • 146
  • 11
    No, you should fix the source of the problem that makes you run out of memory in the first place. – EpicPandaForce Dec 30 '15 at 12:04
  • finishing the activity won't solve your OutOfMemory exception, find that and resolve – droidev Dec 30 '15 at 12:10
  • 1
    @MsYvette, my "reasonable" high rep != expert in android – ERJAN Dec 30 '15 at 12:59
  • 2
    @ERJAN it's not so much about being an expert in android as it is about wanting to avoid a problem instead of solving it, which applies to all programming languages – Tim Dec 30 '15 at 13:02
  • @MsYvette, i actually have another problem - my history game app suddenly kicks out to main tablet screen WITHOUT crashing... it does not even give any error... – ERJAN Dec 30 '15 at 13:11

3 Answers3

1

The usual method for closing an app is to call finish() instead of System.exit

Rem-D
  • 627
  • 6
  • 15
  • 5
    Actually finish just finishes the current activity. It "closes" the app only if there is only 1 activity on the stack – Tim Dec 30 '15 at 12:49
1
Intent intent = new Intent(banana.this, super.class);
intent.putExtra("username1", username1_who_logged);
startActivity(intent);
finish();

You can use finish() method instead of System.exit(1). Finish can close your current activity but exit(1) method close your application.

droidev
  • 7,352
  • 11
  • 62
  • 94
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
1

This is a really bad idea and I'd strongly recommend you do not do it.

Use your logcat to trace where the outOfMemory exception is occurring. Closing activities will make no difference unless it closes you app. The resources will not be resumed by the OS until the app stops running. Also bear in mind many android devices are not long on memory, so it's important that resources are managed well and this is a good programming discipline generally.

So in effect to make your app functional, you're going to need to force it to close, which is not functional (if that makes sense). It's not a logical or viable solution.

The logcat is a useful tool for android, and if you can trace where the exception is, you can address it, as it's not good to have leaky apps. Or it may be some large image, file, or download, I don't know what's causing it.

One thing, is if you find it, post your logcat, the relevant code, we can help you.