I have an activity with video preview displayed via SurfaceView and other views positioned over it. The problem is when user navigates to Settings activity (code below) and comes back then the surfaceview is drawn on top of everything else. This does not happen when user goes to another activity I have, neither when user navigates outside of app eg. to task manager.
Now, you see in code below that I have setContentVIew() call wrapped in conditionals so it is not called every time when onStart() is executed. If its not wrapped in if statements then all works fine, but its causing loosing lots of memory (5MB+) each time onStart() is called. I tried various combinations and nothing seems to work so any help would be much appreciated.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Toast.makeText(this,"Create ", 2000).show();
// set 32 bit window (draw correctly transparent images)
getWindow().getAttributes().format = android.graphics.PixelFormat.RGBA_8888;
// set the layout of the screen based on preferences of the user
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
}
public void onStart()
{
super.onStart();
String syncConnPref = null;
syncConnPref = sharedPref.getString("screensLayouts", "default");
if(syncConnPref.contentEquals("default") && currentlLayout!="default")
{
setContentView(R.layout.fight_recorder_default);
}
else if(syncConnPref.contentEquals("simple") && currentlLayout!="simple")
{
setContentView(R.layout.fight_recorder_simple);
}
// I I uncomment line below so it will be called every time without conditionals above, it works fine but every time onStart() is called I'm losing 5+ MB memory (memory leak?). The preview however shows under the other elements exactly as I need memory leak makes it unusable after few times though
// setContentView(R.layout.fight_recorder_default);
if(getCamera()==null)
{
Toast.makeText(this,"Sorry, camera is not available and fight recording will not be permanently stored",2000).show();
// TODO also in here put some code replacing the background with something nice
return;
}
// now we have camera ready and we need surface to display picture from camera on so
// we instantiate CameraPreviw object which is simply surfaceView containing holder object.
// holder object is the surface where the image will be drawn onto
// this is where camera live cameraPreview will be displayed
cameraPreviewLayout = (FrameLayout) findViewById(id.camera_preview);
cameraPreview = new CameraPreview(this);
// now we add surface view to layout
cameraPreviewLayout.removeAllViews();
cameraPreviewLayout.addView(cameraPreview);
// get layouts prepared for different elements (views)
// this is whole recording screen, as big as screen available
recordingScreenLayout=(FrameLayout) findViewById(R.id.recording_screen);
// this is used to display sores as they are added, it displays like a path
// each score added is a new text view simply and as user undos these are removed one by one
allScoresLayout=(LinearLayout) findViewById(R.id.all_scores);
// layout prepared for controls like record/stop buttons etc
startStopLayout=(RelativeLayout) findViewById(R.id.start_stop_layout);
// set up timer so it can be turned on when needed
//fightTimer=new FightTimer(this);
fightTimer = (FightTimer) findViewById(id.fight_timer);
// get views for displaying scores
score1=(TextView) findViewById(id.score1);
score2=(TextView) findViewById(id.score2);
advantages1=(TextView) findViewById(id.advantages1);
advantages2=(TextView) findViewById(id.advantages2);
penalties1=(TextView) findViewById(id.penalties1);
penalties2=(TextView) findViewById(id.penalties2);
RelativeLayout welcomeScreen=(RelativeLayout) findViewById(id.welcome_screen);
Animation fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);
welcomeScreen.startAnimation(fadeIn);
Toast.makeText(this,"Start ", 2000).show();
animateViews();
}
Settings activity is below, after coming back from this activity surfaceview is drawn on top of other elements.
public class SettingsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(MyFirstAppActivity.getCamera()==null)
{
Toast.makeText(this,"Sorry, camera is not available",2000).show();
return;
}
addPreferencesFromResource(R.xml.preferences);
}
}