我有一项活动,通过SurfaceView显示视频预览,并在其上方显示其他视图。问题是当用户导航到“设置”活动(下面的代码)并返回时,表面视图将绘制在其他所有内容之上。当用户转到我拥有的另一个活动时,当用户在应用程序之外导航时,这不会发生。任务经理。
现在,您在下面的代码中看到我在条件语句中包含了setContentVIew()调用,因此每次执行onStart()时都不会调用它。如果它没有包含在if语句中,那么一切正常,但每次调用onStart()时会导致大量内存(5MB +)丢失。我尝试了各种组合,似乎没有任何工作,所以任何帮助都会非常感激。
@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();
}
设置活动在下面,从此活动返回后,surfaceview将在其他元素之上绘制。
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);
}
}