1

这是我的问题,例如,当我在 android 中创建一个片段时,让我们说:

GastosSectionFragment gastosFragment = new GastosSectionFragment();
Fragment.gastosFragment.darUserID(userKey);

我向该活动发送一个简单的整数,没问题。例如,假设我发送数字 1。它将存储在该类内的属性中,因为我创建了片段。一切都会好起来的,我收到了号码。但是当我旋转手机时,变量“userKEY会丢失,它会转换为cero”我水平旋转手机时无法保存该数字,如何保存该变量的数据。

在这里,我将如何在 Fragment 中接收“userKey”的代码,

public long darUserID(long userKey) {
    // TODO Auto-generated method stub
    return user_id = userKey;
}

以及带有 onCreate 方法的属性。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_section_gastos,
            container, false);

    idStatico = darUserID(user_id);

而且我无法保存那个小数字。这不是一个 FragmentActivity,这个类只是一个FRAGMENT。第一次很好,旋转屏幕丢失了我之前收到的号码。片段没有 OnSaveBundleInstances。

4

3 回答 3

1

片段可以保存user_id在方法中的bundle中,onSaveInstanceState并在方法中改变方向后恢复onCreateView。检查此片段指南,特别是在示例中显示保存片段状态。

活动代码保持不变(我只是更改了方法名称),您的片段应如下所示:

    class GastosSectionFragment extends Fragment {

       private static final String BUNDLE_USER_ID = "BUNDLE_USER_ID";

       private long user_id;

       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.fragment_section_gastos, container, false);

         if (savedInstanceState != null)
             user_id = savedInstanceState.getLong(BUNDLE_USER_ID);

         // now is your user_id restored after orientation change, 
         // so you can do some stuff with it            

         return rootView;
      }

      public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putLong(BUNDLE_USER_ID, user_id);
      }

      public long setUserID(long userKey) {
         this.user_id = userKey;
      }
   }
于 2013-10-12T07:45:39.083 回答
1
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

添加方法覆盖,你变量保存状态

于 2015-08-10T18:50:19.467 回答
0

在KOSO回答之后,这就是我解决问题的方法,希望对其他人有所帮助。

当我第一次创建片段时一切正常,发送 userKey,一切都很好。

GastosSectionFragment gastosFragment = new GastosSectionFragment();
Fragment.gastosFragment.darUserID(userKey);

现在为了解决这个问题,当手机旋转时的片段,例如水平旋转,它将调用覆盖方法OnCreateView()。每次旋转手机时都会调用此方法。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_section_gastos,
    container, false);
//This is not null when we rotate the screen, first we save the Bundle SavedInstance
//After do that we put a integer, in this case the userKey
//then we recover the user key, and everything will work fine.
if (savedInstanceState != null)
{
    this.savedInstanceState = savedInstanceState;
    savedInstanceState.putInt("userKey", (int) idStatico);
    idStatico = savedInstanceState.getInt("userKey");
}
//this will be null for the first time, only the first time this will bse used.
if (savedInstanceState == null)
{
    idStatico = darUserID(user_id);
}
//Do the rest of the work

}
于 2013-10-13T19:14:28.353 回答