使用具有视图上下文的构造函数创建Presentation对象。
例如,在MainActivity中创建Presentation对象:
public class MainActivity extends AppCompatActivity {
private MyPresentation myPresentation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate and show the presentation
if (myPresentation == null) {
myPresentation = new MyPresentation(this, getWindow().getDecorView());
myPresentation.show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Dismiss the presentation when the activity is destroyed
if (myPresentation != null) {
myPresentation.dismiss();
myPresentation = null;
}
}
private static class MyPresentation extends Presentation {
public MyPresentation(Context context, Display display) {
super(context, display);
}
// Other methods for Presentation
}
}
这里的MyPresentation类扩展Presentation类,并在其构造函数中使用Context和Display对象作为参数。这样可以避免任何StrictMode违规错误。在MainActivity中,我们将获取活动上下文和视图层次结构的根视图并传递给MyPresentation构造函数。然后,我们调用MyPresentation的show()方法来显示它。在MainActivity的onDestroy()方法中,我们释放MyPresentation对象。