【问题标题】:How to restore state of an Android View (UI)?如何恢复 Android 视图 (UI) 的状态?
【发布时间】:2011-08-20 18:18:31
【问题描述】:

我正在尝试将我的视图状态保存在一个活动中,并使用 Bundle 将其传递给另一个活动。在第二个活动中,我尝试使用包恢复视图状态。

第一个活动

private View CreateView() {
ScrollView scrollView = new ScrollView(this);
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(android.widget.LinearLayout.VERTICAL);
scrollView.addView(layout);         
Button btn = new Button(this);
btn.setId(100);
btn.setText("Button Text");
btn.setOnClickListener(new ClickListener());
layout.addView(btn);
return scrollView;
}

onCreate

super.onCreate(savedInstanceState);
View v = CreateView();
setContentView(v);

保存状态

SparseArray<Parcelable> array = new SparseArray<Parcelable>();
view.saveHierarchyState(array);
Bundle bundle = new Bundle();
bundle.putSparseParcelableArray("state", array);
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);

创建第二个活动

bundle = this.getIntent().getExtras();        
View view = new View(this);
view.restoreHierarchyState(bundle.getSparseParcelableArray("state"));
setContentView(view.getRootView());   
Button btn = (Button)findViewById(100);

一切正常,无一例外。但是,我面临两个问题: 1. 第二个活动的视图是空白的。虽然我已经恢复了保存状态,但我什么都看不到 2. 第二个activity中按钮(id为100)的实例总是null

在调试时,我可以看到包中的一个值具有 id 100

对于我似乎做错的事情的任何帮助将不胜感激。谢谢

【问题讨论】:

  • 这让我觉得想做一件奇怪的事。原因是什么?
  • @Dave - 同意这是一件奇怪的事情。遗憾的是,我无法在这里讨论其中的“为什么”部分!谢谢!

标签: android


【解决方案1】:

我发现无法在最初创建(或渲染)视图的不同活动中恢复视图。由于 View 不是可序列化的类型,因此也无法完整发送。目前似乎没有任何解决方案(我还没有探索过修改Android源代码的选项)

【讨论】:

    【解决方案2】:

    您为什么要尝试以编程方式创建 View 并将其发送到另一个活动?您可以简单地在两个活动中使用相同的布局,然后只传递支持视图的数据。那也有比使用parcelable更方便的方法吗?

    您能否详细说明您在这里想要实现的目标?或许我们可以给你一个更好的回应......

    【讨论】:

      【解决方案3】:

      您是否尝试过在onSavedInstanceState(...) 中这样做?

      来自Android 文档:

      onSaveInstanceState(Bundle) 在将活动放入之前调用 这样的背景状态,让您可以保存任何动态 将您的活动中的实例状态放入给定的 Bundle 中,稍后再进行 在 onCreate(Bundle) 中收到

      【讨论】:

      • onSaveInstanceState 在 Activity 即将从堆栈中拉出时由 Android 调用。但是,我试图根据应用程序触发器将状态从一个视图传递到另一个视图。谢谢。
      • 为什么不使用Intent
      【解决方案4】:

      我采取的唯一方法是使用 onSaveInstanceState 和 onRestoreInstanceState。

      public void onSaveInstanceState(Bundle outState){
          //save the state of the sign up views!
          Serializable page0fields = sign_in_page.getFields();
          outState.putSerializable("page0fields", page0fields);
          super.onSaveInstanceState(outState);    
      }
      
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState){
          //restore the state of the sign up views!
          sign_in_page.fillFields(
                  savedInstanceState.getSerializable("page0fields")
                  );
          super.onRestoreInstanceState(savedInstanceState);
      }
      

      另一方面,您似乎希望采用更自动化的方法来解决此问题。我不相信您可以任意传递视图,因为它们不可序列化,尽管我可能弄错了。

      【讨论】:

      • 是的,视图不可序列化。这就是我使用 View.saveHierarchyState 的原因,它返回一个 Bundle,而后者又是可序列化的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2017-01-04
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多