Android的 - 保存碎片类成员字段值 [英] Android - save value of member field in Fragment class

查看:203
本文介绍了Android的 - 保存碎片类成员字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用布尔片段类每次<$ C $做点什么C>片段显示。

I'm trying to do something using a boolean in a Fragment class each time the Fragment is displayed.

我的应用程序启动时,将打开 FirstFragment 布尔首次始终是真正,然后我有检查其值的如果子句:

My app launches, opens the FirstFragment and the boolean for the first time is always true, then I have an if clause that checks its value:

if (FirstTime) {
    FirstTime = false;
} else {
    // Other stuff here, cause it's not true.
}

然后,在第一时间,当 FirstTime 真正,我做的东西一样去到另一个片段。当我回到片段1 和我的的onCreate(),我也一样。它总是真正,似乎让人耳目一新,或东西。

Then, on the first time, when FirstTime is true, I do stuff like go to another Fragment. and when I return to Fragment1 and on my onCreate(), I do the same. It's always true, seems that it's refreshing or something.

后来我想这可能是与片段的一个问题,每次我在片段1 preSS ,将重新启动或什么的。然后,我添加了一个getter和setter我的 MainActivity

Then I thought that could be a problem with Fragment, and every time I press on Fragment1, it restarts or something. Then, I've added a getter and setter in my MainActivity:

public Boolean getFirstTime() {
    return FirstTime;
}

public void setFirstTime(Boolean FirstTime) {
    this.FirstTime = FirstTime;
}

在这里开赛以来,这是真的,然后,我改变了我的code从片段1 为:

if (((MainActivity) getActivity()).getFirstTime())
    ((MainActivity) getActivity()).setFirstTime(false);
} else {
    // Other stuff here, cause it's not true,
}

但是,它仍然说这是事实。

However, it's still saying that's true.

什么,我做错了还是什么我误解了有关片段?结果
有没有办法做到这一点?

What I'm doing wrong or what I misunderstood about Fragments?
Is there any way to do it?

推荐答案

您已经取得了片段实例,只要应用程序是活的继续存在的假设。这是一个合理的假设,你的做法将正常工作,如果这个假设是真的。

You have made an assumption that the Fragment instance remains in existence as long as the app is alive. It is a reasonable assumption, and your approach would work fine if that assumption were true.

不幸的是,片段时,已在后台被破坏,并创造重新当它返回到前台。这就是为什么它似乎刷新。同样不是一个活动也是如此。当活动回落到背景,它不立即销毁。相反,它被保持在当前任务的一段时间backstack,如果它返回到前台,它是相同的实例

Unfortunately, a Fragment is destroyed when it recedes into the background, and created anew when it returns to the foreground. This is why it appears to "refresh". The same is not true of an Activity. When an Activity recedes into the background, it is not destroyed immediately. Rather, it is maintained on the current task's backstack for some time, and if it returns to the foreground, it is the same instance.

要解决这一问题,有四种不同的方式:

To combat this problem, there are four different ways:


  • 声明 FirstTime 静态。这的的工作。我以前用过这个。然而,这仅应在极端的情况下使用时,存在是绝对必要的,以preserve成员字段的值,并且仅当没有其它的方法是可用的。制作一个变量静态引出一个经典的内存泄漏。

  • 片段使用<一个保存 FirstTime 的价值href=\"http://developer.android.com/reference/android/app/Fragment.html#onSaveInstanceState%28android.os.Bundle%29\"相对=nofollow> 的onSaveInstanceState()

  • Declare FirstTime as static. This should work. I've used this before. However, this should only be used in extreme cases when there is an absolute necessity to preserve the value of a member field, and only when no other way is available. Making a variable static leads to a classic memory leak.
  • Save the value of FirstTime in your Fragment using onSaveInstanceState():

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean("FirstTime", FirstTime);
}

和检索的onCreate()中的值

@Override
public void onCreate (Bundle savedInstanceState){
    super.onCreate();
    FirstTime = savedInstanceState.getBoolean("FirstTime");
}


  • 声明 FirstTime 在全局常量类,而不是把它在片段

  • Declare FirstTime in a global constants class instead of putting it in the Fragment:

    public class GlobalConstants{
    
        public static boolean FirstTime = true;
        // other global constants ...
    
    }
    

    和访问它,在你的片段是这样的:

    and access it in your Fragment like this:

    if (GlobalConstants.FirstTime) {
        GlobalConstants.FirstTime = false;
    } else {
        //Other stuff here cause it's not true
    }
    


  • FirstTime 的值=htt​​p://developer.android.com/reference/android/content/Shared$p $ pferences.html相对=nofollow> 共享preference

  • Save the value of FirstTime in a SharedPreference:

    SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putBoolean("FirstTime", FirstTime);
    editor.commit();
    

    和以这种方式检索其值

    SharedPreferences sp = getActivity().getPreferences(Context.MODE_PRIVATE);
    FirstTime = sp.getBoolean("FirstTime", true);
    


  • 而应用程序是活的前三种方法将保持 FirstTime 的价值。第四种方法将preserve FirstTime 之外的应用程序的生命周期,即当应用程序重新启动时, FirstTime 真正这取决于它的值设置的最后一个程序退出之前。

    The first three methods will maintain the value of FirstTime while the application is alive. The fourth method will preserve the value of FirstTime beyond the lifetime of the application, i.e. when the app restarts, FirstTime will be true or false depending on what its value was last set before the app exited.

    参考文献:

    1 处理片段生命周期

    2 保存键 - 值集

    3。 可见性和寿命

    编辑:

    要了解如何使用的onSaveInstanceState(),请访问以下链接:

    To understand how to use onSaveInstanceState(), see the following links:

    1 保存(和检索)的Andr​​oid实例状态

    2 Once所有,如何正确保存片段的实例状态

    3。 处理配置更改

    它的的混乱,但一旦你明白了这将是对你有用。

    It is confusing, but it will be useful to you once you understand it.

    这篇关于Android的 - 保存碎片类成员字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆