我有一个应用程序,当旋转屏幕时,该应用程序应将所有显示的数据保留到回收站中。回收者从“自定义阵列列表”接收数据。还有第二个变量,用于在适配器内部设置VideoView的Uri。
已经尝试过onSave和onRestoreIntance。也许我用错了他们。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Variables-----------------------------------------
recyclerView = findViewById(R.id.recyclerView);
Button video = findViewById(R.id.video);
Button camera = findViewById(R.id.camera);
Button send = findViewById(R.id.send);
final EditText editText = findViewById(R.id.editText);
// Layout Manager------------------------------------------------
linearLayoutManager = new LinearLayoutManager(MainActivity.this);
linearLayoutManager.setStackFromEnd(true);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setItemAnimator(itemAnimator);
// Adapter-----------------------------------------
//adapter.notifyDataSetChanged();
adapter = new myAdapter(dati, this);
recyclerView.setAdapter(adapter);
// Click Listener Video button----------------------------------------
video.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,0);
}
});
// Click Listener Camera button--------------------------------------
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,1);
}
});
// Click Listener Send button-----------------------------------------
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String string = editText.getText().toString();
dati.add(new ModelloDati(0,string));
adapter.notifyItemInserted(dati.size());
editText.getText().clear();
recyclerView.smoothScrollToPosition(dati.size());
closeKeyboard();
}
});
if(savedInstanceState != null)
linearLayoutManager.onRestoreInstanceState(
savedInstanceState.getParcelable("STATO_LISTA"));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 0:
try {
Uri contentURI = data.getData();
passUri = contentURI;
String recordedVideoPath = getPath(contentURI);
saveVideoToInternalStorage(recordedVideoPath);
dati.add(new ModelloDati(2, contentURI));
adapter.notifyItemInserted(dati.size());
recyclerView.smoothScrollToPosition(dati.size());
}catch (Throwable o){Log.i("CAM","User aborted action");}
case 1:
try {
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
dati.add(new ModelloDati(1,bitmap));
adapter.notifyItemInserted(dati.size());
recyclerView.smoothScrollToPosition(dati.size());
}catch(Throwable o){
Log.i("CAM","User aborted action");
}
}
}
@Override
protected void onResume() {
super.onResume();
if (saveList != null) {
linearLayoutManager.onRestoreInstanceState(saveList);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveList = linearLayoutManager.onSaveInstanceState();
outState.putParcelable("STATO_LISTA",saveList);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState != null)
savedInstanceState.getParcelable("STATO_LISTA");
}
}
答案 0 :(得分:0)
Android提供了Architecture components,可帮助您处理此类情况。 例如ViewModel
Android框架管理UI控制器的生命周期,例如活动和片段。该框架可能会决定完全响应您无法控制的某些用户操作或设备事件来销毁或重新创建UI控制器。
如果系统销毁或重新创建UI控制器,则您存储在其中的所有与UI相关的瞬时数据都将丢失。例如,您的应用可能在其活动之一中包含用户列表。重新创建活动以进行配置更改时,新活动必须重新获取用户列表。对于简单数据,活动可以使用onSaveInstanceState()方法并从onCreate()中的包中还原其数据,但是此方法仅适用于可以先序列化然后反序列化的少量数据,而不适合潜在的大量数据例如用户列表或位图。
因此,基本上,您可以将适配器的数据集保存在ViewModel中,并在onCreate
内获取数据并将其设置到适配器中。
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
new adapter(model.getData(),this); // just for the example.