我有一个通过 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();
}
设置活动如下,从该活动返回后,表面视图被绘制在其他元素之上。
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);
}
}