- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个应用程序,当屏幕旋转时,它应该将所有显示的数据保留到回收器中。回收器从自定义数组列表接收数据。还有第二个变量用于设置适配器内 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");
}
}
最佳答案
Android 优惠 Architecture components这可以帮助您处理此类情况。例如ViewModel
The Android framework manages the lifecycles of UI controllers, such as activities and fragments. The framework may decide to destroy or re-create a UI controller in response to certain user actions or device events that are completely out of your control.
If the system destroys or re-creates a UI controller, any transient UI-related data you store in them is lost. For example, your app may include a list of users in one of its activities. When the activity is re-created for a configuration change, the new activity has to re-fetch the list of users. For simple data, the activity can use the onSaveInstanceState() method and restore its data from the bundle in onCreate(), but this approach is only suitable for small amounts of data that can be serialized then deserialized, not for potentially large amounts of data like a list of users or bitmaps.
所以基本上,您可以在 ViewModel 中保存适配器的数据集,并在 onCreate
中获取数据并将其设置到您的适配器中。
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class);
new adapter(model.getData(),this); // just for the example.
关于java - 如何将 onSaveInstance 方法与 Recyclerview 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56328714/
我有一个 android 应用程序,想在方向更改时保存实例的状态。我的 Activity 包含原始数据类型和一个用户定义的位图和字符串数组列表。哪种方法更适合解决我的问题? OnSaveInstanc
我有一个应用程序,当屏幕旋转时,它应该将所有显示的数据保留到回收器中。回收器从自定义数组列表接收数据。还有第二个变量用于设置适配器内 VideoView 的 Uri。 已经尝试过 onSave 和 o
这个问题在这里已经有了答案: Handle screen rotation without losing data - Android (8 个答案) 关闭 4 年前。 我已经测试了所有内容并花了最
好的,我已经覆盖了 OnSaveInstanceState 和 OnRestoreInstanceState,但在 OnCreate 中, bundle 为空。我已经解决了所有其他相关问题,但它们对我
我有 FragmentA 由 ActivityA 托管。当用户从选项菜单中选择一个项目时,将启动承载 FragmentB 的 ActivityB。现在,我想通过覆盖 onSaveInstanceSta
为什么我需要使用 setRetainInstance() 或 onSaveInstance() 来保存状态,而我可以使用 android:configChanges="keyboard|orienta
我是一名优秀的程序员,十分优秀!