Android Crash When App Is Closed and Reopened

Android crash when app is closed and reopened

I've answered a question like this here.

The error you're getting is probably caused by your Thread (without seeing the full Logcat it's hard to tell though). You're starting it every time the surface is created, which will make your application crash because you can't call Thread.start() twice. Look at my link above for a more in-depth description of the problem and how you should solve it.

Since my explanation wasn't enough I will post the whole solution:

Inside your Runnable/Thread:

private Object mPauseLock = new Object();  
private boolean mPaused;

// Constructor stuff.

// This should be after your drawing/update code inside your thread's run() code.
synchronized (mPauseLock) {
while (mPaused) {
try {
mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}

// Two methods for your Runnable/Thread class to manage the thread properly.
public void onPause() {
synchronized (mPauseLock) {
mPaused = true;
}
}

public void onResume() {
synchronized (mPauseLock) {
mPaused = false;
mPauseLock.notifyAll();
}
}

In your SurfaceView class:

private boolean mGameIsRunning;

@Override
public void surfaceCreated(SurfaceHolder holder) {
// Your own start method.
start();
}

public void start() {
if (!mGameIsRunning) {
thread.start();
mGameIsRunning = true;
} else {
thread.onResume();
}
}

Android App Crashes Once Reopened

this may or may not solve your issue, but when you call your 'gamble' activity from your main activity, you should be using startActivityForResult() and register a callback receiver to handle the result...

i.e. you're currently 'starting' your main activity over itself instead of just passing results back to it via setResult

see:

https://developer.android.com/training/basics/intents/result.html

and

https://developer.android.com/reference/android/os/ResultReceiver.html

UPDATE:

consider creating a static final request code you can use:

public static final int REQUEST_CODE_GAMBLE = 172;

change your getCoin methods to startActivityForResult(), using your static request code:

public void getOneCoin(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 1);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivityForResult(intent, REQUEST_CODE_GAMBLE);
}

then override onActivityResult() in your MainActivity:

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == REQUEST_CODE_GAMBLE){
if(resultCode == RESULT_OK){

//do all that same stuff you were doing in onCreate() in your MainActivity
//i.e. :
coins = intent.getIntExtra("COINS", 1);
//etc

}
}

}

then in your winner() and loser() methods in the gamble activity, simply set the result and call finish():

public void winner()
{
Intent oldintent = getIntent();
Bundle oldbundle = oldintent.getExtras();
int gambleamount = oldintent.getIntExtra("GAMBLE", 1);
int coins = oldintent.getIntExtra("COINS", 1);
ArrayList<Integer> coinHistory = oldbundle.getIntegerArrayList("COIN_HISTORY");
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinHistory);
intent.putExtra("WINNER", true);
intent.putExtra("GAMBLE", gambleamount);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
getParent().setResult(Activity.RESULT_OK, intent);
finish();
}

When your gamble activity "finishes", it will send all that intent data to your MainActivity's new onActivityResult() method, and you can do with it what you'd like there

Android: App crashes after closing and reopening

Note that

Caused by: java.lang.IllegalStateException
at android.media.MediaPlayer._start(Native Method)

When app is closed, I think, MediaPlayer's state is not properly handled. On relaunching app, media player's state is different whic does not support to start playing.

I think, when you close app, onPause() is invoked, you release MediaPlayer instance. But when you relaunch app (maybe you launch app from background from task manager), there is no initialized MediaPlayer and you try to play media.

app crashes when closing / reopening then pressing button

It appears that the onPause in your activity is being called, and you're releasing the Camera - this is good. However, you never re-initialize it.

The Android developer guide has this exact example.

Basically, you need to do something like this:

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

if (camera != null) {
camera.release();
camera = null;
}
}

@Override
protected void onResume() {
super.onResume();

if (camera == null) {
initializeCamera();
}
}

Then you move all the camera initialization code from your onCreate into a new initializeCamera method.

Note you should not initialize the camera in onCreate, since it is being done in onResume (which is called even on first load).

Android: Unexplained crashes when the app is reopened after a while

Activities not on screen are frequently killed by Android for memory. When coming back to an Activity via recents, the most recent activity will be launched but if it was killed it will have no context- it will get the intent it was launched with but have no static variables or singletons assigned to previous values. This causes problems for a lot of apps. The solution is to either allow any activity to fill out these values somehow, or to detect this case and launch an appropriate activity with CLEAR_TOP set to kill the old state.

Oh, and to reproduce it all you need is force stop from the activites manager settings. Go to activity D, then force stop. Then return via recents.

As an example- I have an app with a LoginActivity, and a MainActivity. MainActivity assumes LoginActivity has set up a user singleton. My code in MainActivity is:

public void onCreate(Bundle bundle){
super.onCreate(bundle);
if(User.getCurrentUser() == null){
Intent intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return;
}
//Continue on with onCreate for this activity
}

This will relaunch the login activity if the User object wasn't set up.

Android app crashes when reopening and replacing fragment

I am not sure what is the context of your use case, but calling fragmentTransaction.commitAllowStateLoss(); should not cause the crash anymore. However, you need to assume the risk that your state info will be lost on fragment.


Also, this line currentFragment = fragment; seems to me a cause of memory leak. If Android wants to cleanup the fragment you will prevent it by keeping a strong reference to the fragment. Don't use it ...



Related Topics



Leave a reply



Submit