Bug en sorti de sous activité
Bonjour.
J'ai un bug qui ne survient que de temps en temps, lorsque je sorts d'une sous-activité pour retourner à l'activité principale.
J'ai pu constaté que cela provenait, ou découlait de ce code :
Code:
1 2
|
c.drawBitmap(background,0,0,null); |
Voici mon main :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
public class MainActivity extends Activity implements View.OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = ((Button)this.findViewById(R.id.button1));
button.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v)
{
Intent intent = new Intent(this, Activity2.class);
this.startActivityForResult(intent, 1 );
}
} |
voici ma sous-activité :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
public class Activity2 extends Activity
{
GameContent gameContent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gameContent = new GameContent(this);
setContentView(gameContent);
}
} |
voici la class GameContent, qui est la vue du jeux de la sous activité :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
public class GameContent extends SurfaceView implements SurfaceHolder.Callback
{
private GameLoop gameLoop;
private Context context ;
private int screenX, screenY ;
private Bitmap background ;
private Builder builder;
public GameContent(Context c)
{
super(c);
context = c ;
gameLoop = new GameLoop (this) ;
getHolder().addCallback(this);
background = BitmapFactory.decodeResource(c.getResources(), R.drawable.background_1);
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
if(gameLoop.getState()==Thread.State.TERMINATED)
{
gameLoop = new GameLoop(this);
}
gameLoop.setRunning(true);
gameLoop.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
screenX = width;
screenY = height;
background = Bitmap.createScaledBitmap(background,width,height,true);
builder = new Builder( context, width, height);
}
public void draw( Canvas c )
{
c.drawBitmap(background,0,0,null);
//builder.update(c);
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
boolean retry = true;
gameLoop.setRunning(false);
while (retry)
{
try
{
gameLoop.join();
retry = false;
}
catch (InterruptedException e) {}
}
}
} |
Et enfin le GameLoop, qui gère le temps :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
public class GameLoop extends Thread {
private final static int FRAMES_PER_SECOND = 30 ;
private final static int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
private final GameContent view;
private boolean running = false;
public GameLoop(GameContent view){
this.view = view;
}
public void setRunning(boolean run){
running = run;
}
@Override
public void run(){
long startTime;
long sleepTime;
while (running)
{
startTime = System.currentTimeMillis();
Canvas c = null;
try
{
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()){view.draw(c);}
}
finally
{
if(c != null){view.getHolder().unlockCanvasAndPost(c);}
}
sleepTime = SKIP_TICKS-(System.currentTimeMillis()-startTime);
try
{
if (sleepTime >= 0)
{
sleep(sleepTime);
}
}
catch (Exception e){}
}
}
} |
Je me demande s'il y a pas une démarche que je n'ai pas fait correctement en ce qui concerne la fermeture de la sous activité, car c'est là qu'intervient le bug ( ce environ 1 fois sur 3 ).
Merci beaucoup si vous pouvez m'aider :P