如何存储值并在方向改变时检索它?

发布于 2024-11-16 14:54:59 字数 1054 浏览 1 评论 0原文

我检查了很多帖子,并据此我已经完成了方向更改的编码。当方向改变时,问题是我无法检索 TextView 中输入的值。谁能告诉我哪里错了?

编码:

在我添加的相应活动的清单文件中:

android:configChanges="orientation|keyboardHidden"

在活动中,我添加了以下方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_screen);
    //Initialized the widgets
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //have written separate layout files for portrait and landscape
    setContentView(R.layout.home_screen); 
    //Initialized the widgets again
    retrieveSavedState();  //sets the TextViews again
}

@Override
protected void onPause() {
    super.onPause();
    saveState(); //save the TextView values
}

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    saveState();
}

@Override
protected void onResume() {
    super.onResume();
    retrieveSavedState();
}

I have checked many posts, and as per that I have done coding for the orientation change. When orientation changes, the problem is I am not able to retrieve the entered values inside TextViews. Can anyone plz tell where did I go wrong?

Coding:

In manifest file for the corresponding activity I added:

android:configChanges="orientation|keyboardHidden"

In activity, I added the following methods:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_screen);
    //Initialized the widgets
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //have written separate layout files for portrait and landscape
    setContentView(R.layout.home_screen); 
    //Initialized the widgets again
    retrieveSavedState();  //sets the TextViews again
}

@Override
protected void onPause() {
    super.onPause();
    saveState(); //save the TextView values
}

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    saveState();
}

@Override
protected void onResume() {
    super.onResume();
    retrieveSavedState();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2024-11-23 14:54:59
@Override
public Object onRetainNonConfigurationInstance() 
{
    final MyDataObject data = collectMyLoadedData();
    return data;
}

上面的方法可用于保存任何对象来保存数据...

@Override
public void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
    if (data == null) {
        data = loadMyData();
    }
    ...
}

并且您可以在 onCreate() 中检索该对象,当方向改变时我们将调用该对象..从该对象读取数据后,您可以可以在任何你想要的地方使用它..

http://developer.android.com/guide/topics/resources/runtime-changes.html

http://developer.android.com/guide/topics/resources/runtime-changes.html

其他机制也有,但这是最好的方法据我所知..您还可以使用共享首选项来存储和检索数据...

http ://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html

@Override
public Object onRetainNonConfigurationInstance() 
{
    final MyDataObject data = collectMyLoadedData();
    return data;
}

the above method can be used to save any object to save data...

@Override
public void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
    if (data == null) {
        data = loadMyData();
    }
    ...
}

and u can retreive that object in the onCreate() which will we called when orientation changes.. after reading data from that object you can use it where ever you want..

http://developer.android.com/guide/topics/resources/runtime-changes.html

http://developer.android.com/guide/topics/resources/runtime-changes.html

other mechanisms are also there but this is the best way for my knowledge.. you can also use shared preferences to store and retrieve data...

http://saigeethamn.blogspot.com/2009/10/shared-preferences-android-developer.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文